1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 8 “Character Arrays and Strings” Prepared by: Prof. Ajay M. Patel CE, IDS, NU.
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
A First Book of ANSI C Fourth Edition
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
Programming and Data Structure
CS111 Computer Programming
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
Week 9 – Lesson 1 Arrays – Character Strings
CprE 185: Intro to Problem Solving (using C)
Chapter 2 Array and String Visit to more Learning Resources.
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C++ Programming Lecture 20 Strings
Strings in C Array of characters is called a string.
Strings #include <stdio.h>
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
Presentation transcript:

1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions 8.5Table of Strings

2 Objectives In this chapter, we will learn: –To be able to understand the difference between a character array and a string –To be able to use the string and character input/output functions of the standard input/output library ( stdio). –To be able to use the string processing functions of the string handling library (string).

3 8.1Introduction Review questions –How to declare a character array? –What is a string constant? –What’s the difference between ‘x’ and “x”?

4 8.1Introduction A string constant is a sequence of characters enclosed in double quote. C does not support strings as a data type. We can represent strings as character arrays. The common operations performed on character strings include: –Reading and writing strings –Combining strings together –Copying one string to another –Comparing strings for equality –……

5 8.2 Declaring and initializing string String definitions ① Define as a character array char string_name [size] ; –For example, char city[10] ; ② Define as a variable of type char * (Chapter 11)

6 8.2 Declaring and initializing string String initializations –Character arrays may be initialized when they are declared. ① char city [9] = { 'N','e','w',' ','Y','o','r','k',\0'}; ② char city [9] = “New York” ; ③ char city [ ]= {'N','e','w',' ','Y','o','r','k',\0'}; NewYork\0 ④ char str[10] = “Good” ; Good\0

7 8.2 Declaring and initializing string String initializations ① char s1 [5] = {'H', ‘E', ‘L', ‘L', ‘O'}; ② char s2 [ ] = “HELLO” ; –Is s1 a character string? HELLO Strings represented as character arrays end with '\0‘ The size should be equal to the maximum number of characters in the string plus one. s1 HELLO\0 s2

8 8.2 Declaring and initializing string Note ① The following declaration is illegal char str[3] = “Good”; ② we cannot separate the initialization from declaration. char str3 [5] ; str3 = ”GOOD”; /* not allowed. */ –An array name cannot be used as the left operand of an assignment operator.

9 8.3 Input/output of string 1.Using scanf( ) and printf( ) functions –%s format /* program str1.c */ #include int main() { char name[20]; printf("What's your name?\n"); scanf("%s",name); printf("Your name is %s\n",name); return 0; }

Input/output of string 1.Using scanf( ) and printf( ) functions –%s format Do not need & Leave room in the array for '\0' %s terminates on the first white space

Input/output of string 2. Using getchar( ) and putchar() functions /* program str2.c, read string using getchar() */ #include int main() { char name[20],ch; int i=0; printf("What's your name?\n"); while((ch=getchar())!='\n') { name[i]=ch; i++; } name[i]='\0'; printf("Your name is "); for(i=0;name[i]!='\0';i++) putchar(name[i]); return 0; }

Input/output of string 3. Using gets( ) and puts( ) function gets( str ) ; puts( str ); –str is the name of a character array. –For example, char line[80]; gets ( line) ; puts( line ); –reads a line of text from the keyboard and output it

13 8.4String-handling functions The C library supports a large number of string-handling functions that can be used to carry out many of the string manipulations. Following are the most commonly used string-handling functions which are contained in FunctionAction strcat() concatenates two strings strcmp() compares two strings strcpy() copies one string over another strlen() finds the length of a string

14 8.4String-handling functions 1.strcat( ) function –To joins two string together, it takes the following form: strcat ( string1, string2 ) ; –String2 will be appended to string1, while the ‘\0’ at the end of string1 will be removed and placing string2 from there. –The size of string1 must be large enough to contain the final string. –string2 may be a character array or a string constant while string1 must be a character array.

15 8.4String-handling functions 1.strcat( ) function, for example –char str1[10] = “very ” ; –char str2[ ] = “good” ; very\0 str1 = good\0 str2 = –strcat ( str1, str2 ); verygood\0 str1 =

16 8.4String-handling functions 2.strcmp( ) function –To compares two strings, it takes the following form: x = strcmp ( string1, string2 ) ; –string1 and string2 may be character arrays or string constants, x is an integer variable. –x will be a negative number if the string1 less than string2 –x will be 0 if they are equal –x will be a positive number if string1 greater than string2.

17 8.4String-handling functions 2.strcmp( ) function –x = strcmp(“compare”, “computer”); compare\0 computer – The following statements are used for determining whether two strings are equal or not if ( strcmp( str1,str2) == 0 ) printf(“%s is equal to %s”,str1,str2);

18 8.4String-handling functions 3.strcpy( ) function –The strcpy function works almost like a string- assignment operator. It takes the form strcpy(string1, string2); –It assigns the contents of string2 to string1. string2 may be a character array variable or a string constant, string1 must be a character array. –The contents of string1 will be covered. –the size of string1 must be large enough to receive string2.

19 8.4String-handling functions 3.strcpy( ) function –char str1[20]=“China”; –char str2[]=“Chang Chun”; –strcpy( str1, str2); –strcpy ( str2, “Hello”);

20 8.4String-handling functions 4.strlen( ) function –This function counts and returns the actual number of characters in a string, not including ‘\0’. –It takes the form n = strlen ( string ) ; –where n is an integer variable, which receives the value of the length of the string. –It ends at the first null character. –n = strlen (“China”);

21 8.4String-handling functions (self-study) Other string functions –The header file contains many more string manipulation functions. –(1) strncpy( ) –(2) strncat( ) –(3) strstr( )

22 Summarize What’s difference between a character array and a string? How to initialize a character array? Input/output of string. Some functions in string.h

23 s1,s2 and s3 are three string variables. Write a program to read two string constants into s1 and s2 and compare whether they are equal or not. If they are not, join them together. Then copy the contents of s1 to the variable s3. At the end, the program should print the contents of all the three variables and their lengths. Example 8.8

24 Algorithm Read s1, s2 comparing s1 and s2, x = strcmp(s1,s2) if ( x ) join s1 and s2 copy s1 to s3 evaluate the length of s1, s2 and s3 print s1, s2, s3 and their length

25 Case Study1——counting words in a text One of the practical applications of string manipulations is counting the words in a text. We assume that a word is a sequence of any characters, except escape characters and blanks, and that two words are separated by one blank character. The algorithm for counting words is as follows: 1. Read a line of text. 2. Beginning from the first character in the line, look for a blank. If a blank is found, increment words by Continue steps 1 and 2 until the last line is completed.

Table of strings Two-dimensional character array can be used to store a list strings. For example, char weeks[7][10] = {“Sunday”,”Monday”,”Tuesday”,”Wednesday”, ”Thursday”,”Friday”,”Saturday”}; It stores as below Sunday\0 Monday Tuesday Wednesday Thursday Friday Saturday

27 Write a program that would sort a list of names in alphabetical order. –read the list of names –sort in alphabetical order –print the list of names Example 8.9

28 Case Study2 Processing of a customer list Telephone numbers of important customers are recorder as follows: It is desired to prepare a revised alphabetical list with surname(last name) first, followed by a comma and initials of the first and middle names. Full nameTelephone number Joseph Louis Lagrange Jean Robert Argand Carl Freidrich Gauss ……

29 –read the list of first_name, second_name,surname and telephone –converting full name to surname with initials –order on alphabetical ordering of surnames –print result

30 Assignment: –Programming exercise 8.3/8.6, 8.8