Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions."— Presentation transcript:

1 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 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 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 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 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 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 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 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 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; }

10 10 8.3 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

11 11 8.3 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; }

12 12 8.3 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 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 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 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 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 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 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 19 8.4String-handling functions 3.strcpy( ) function –char str1[20]=“China”; –char str2[]=“Chang Chun”; –strcpy( str1, str2); –strcpy ( str2, “Hello”);

20 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 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 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 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 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 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 1. 3. Continue steps 1 and 2 until the last line is completed.

26 26 8.9 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 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 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 Lagrange869245 Jean Robert Argand900823 Carl Freidrich Gauss806788 ……

29 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 30 Assignment: –Programming exercise 8.3/8.6, 8.8


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

Similar presentations


Ads by Google