1 Pointers and Strings Outline Introduction to Character

  • Slides: 25
Download presentation
1 Pointers and Strings Outline Introduction to Character and String Processing Fundamentals of Characters

1 Pointers and Strings Outline Introduction to Character and String Processing Fundamentals of Characters and Strings String Manipulation Functions of the String. Handling Library 2003 Prentice Hall, Inc. All rights reserved.

2 Fundamentals of Characters and Strings • Character constant – Integer value represented as

2 Fundamentals of Characters and Strings • Character constant – Integer value represented as character in single quotes – 'z' is integer value of z • 122 in ASCII • String – Series of characters treated as single unit – Can include letters, digits, special characters +, -, *. . . – String literal (string constants) • Enclosed in double quotes, for example: "I like C++" – Array of characters, ends with null character '' – String is constant pointer • Pointer to string’s first character – Like arrays 2003 Prentice Hall, Inc. All rights reserved.

3 Fundamentals of Characters and Strings • String assignment – Character array • char

3 Fundamentals of Characters and Strings • String assignment – Character array • char color[] = "blue"; – Creates 5 element char array color • last element is '' – Variable of type char * • char *color. Ptr = "blue"; – Creates pointer color. Ptr to letter b in string “blue” • “blue” somewhere in memory – Alternative for character array • char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘’ }; 2003 Prentice Hall, Inc. All rights reserved.

4 Fundamentals of Characters and Strings • Reading strings – Assign input to character

4 Fundamentals of Characters and Strings • Reading strings – Assign input to character array word[ 20 ] cin >> word • Reads characters until whitespace or EOF • String could exceed array size! cin >> setw( 20 ) >> word; Reads 19 characters (space reserved for '') 2003 Prentice Hall, Inc. All rights reserved.

5 Fundamentals of Characters and Strings • cin. getline – Read line of text

5 Fundamentals of Characters and Strings • cin. getline – Read line of text (including white space) – cin. getline( array, size, delimiter ); – Copies input into specified array until either • One less than size is reached • delimiter character is input – Example char sentence[ 80 ]; cin. getline( sentence, 80, 'n' ); 2003 Prentice Hall, Inc. All rights reserved.

String Manipulation Functions of the String -handling Library • String handling library <cstring> provides

String Manipulation Functions of the String -handling Library • String handling library <cstring> provides functions to – – Manipulate string data Compare strings Search strings for characters and other strings Tokenize strings (separate strings into logical pieces) 2003 Prentice Hall, Inc. All rights reserved. 6

String Manipulation Functions of the String -handling Library char *strcpy( char *s 1, const

String Manipulation Functions of the String -handling Library char *strcpy( char *s 1, const char *s 2 ); Copies the string s 2 into the character array s 1. The value of s 1 is returned. char *strncpy( char *s 1, const char *s 2, size_t n ); Copies at most n characters of the string s 2 into the character array s 1. The value of s 1 is returned. char *strcat( char *s 1, const char *s 2 ); Appends the string s 2 to the string s 1. The first character of s 2 overwrites the terminating null character of s 1. The value of s 1 is returned. char *strncat( char *s 1, const char *s 2, size_t n ); Appends at most n characters of string s 2 to string s 1. The first character of s 2 overwrites the terminating null character of s 1. The value of s 1 is returned. int strcmp( const char *s 1, const char *s 2 ); Compares the string s 1 with the string s 2. The function returns a value of zero, less than zero or greater than zero if s 1 is equal to, less than or greater than s 2, respectively. 2003 Prentice Hall, Inc. All rights reserved. 7

String Manipulation Functions of the String -handling Library int strncmp( const char *s 1,

String Manipulation Functions of the String -handling Library int strncmp( const char *s 1, const char *s 2, size_t n ); Compares up to n characters of the string s 1 with the string s 2. The function returns zero, less than zero or greater than zero if s 1 is equal to, less than or greater than s 2, respectively. char *strtok( char *s 1, const char *s 2 ); A sequence of calls to strtok breaks string s 1 into “tokens”—logical pieces such as words in a line of text—delimited by characters contained in string s 2. The first call contains s 1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current to ken is returned by each call. If there are no more tokens when the function is called, NULL is returned. size_t strlen( const char *s ); Determines the length of string s. The number of characters preceding the terminating null character is returned. 2003 Prentice Hall, Inc. All rights reserved. 8

String Manipulation Functions of the String -handling Library • Copying strings – char *strcpy(

String Manipulation Functions of the String -handling Library • Copying strings – char *strcpy( char *s 1, const char *s 2 ) • Copies second argument into first argument – First argument must be large enough to store string and terminating null character – char *strncpy( char *s 1, const char *s 2, size_t n ) • Specifies number of characters to be copied from string into array • Does not necessarily copy terminating null character 2003 Prentice Hall, Inc. All rights reserved. 9

1 2 3 // Fig. 8. 31: fig 08_31. cpp // Using strcpy and

1 2 3 // Fig. 8. 31: fig 08_31. cpp // Using strcpy and strncpy. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 14 int main() { char x[] = "Happy Birthday to You"; Copy entire string char y[ 25 ]; into array y. char z[ 15 ]; 15 16 17 18 19 20 21 22 23 24 25 strcpy( y, x ); Outline <cstring> contains prototypes for strcpy and strncpy. // prototypes for strcpy and strncpy in array x // copy contents of x into y cout << "The string in array x is: " << x 14 characters << "n. The string in array y is: " Copy << y first << 'n'; of array x into array Append terminating nully. Note that // copy first 14 characters of x into zthis does not write terminating character. strncpy( z, x, 14 ); // does not copy null character. z[ 14 ] = ''; // append '' to z's contents cout << "The string in array z is: " << z << endl; 2003 Prentice Hall, Inc. All rights reserved. 10

26 27 28 29 return 0; // indicates successful termination } // end main

26 27 28 29 return 0; // indicates successful termination } // end main The string in array x is: Happy Birthday to You The string in array y is: Happy Birthday to You The string in array z is: Happy Birthday Outline String to copy. Copied string using strcpy. Copied first 14 characters using strncpy. 2003 Prentice Hall, Inc. All rights reserved. 11

String Manipulation Functions of the String -handling Library • Concatenating strings – char *strcat(

String Manipulation Functions of the String -handling Library • Concatenating strings – char *strcat( char *s 1, const char *s 2 ) • Appends second argument to first argument • First character of second argument replaces null character terminating first argument • Ensure first argument large enough to store concatenated result and null character – char *strncat( char *s 1, const char *s 2, size_t n ) • Appends specified number of characters from second argument to first argument • Appends terminating null character to result 2003 Prentice Hall, Inc. All rights reserved. 12

1 2 3 // Fig. 8. 32: fig 08_32. cpp // Using strcat and

1 2 3 // Fig. 8. 32: fig 08_32. cpp // Using strcat and strncat. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 14 int main() { char s 1[ 20 ] = "Happy "; char s 2[] = "New Year "; char s 3[ 40 ] = ""; Outline <cstring> contains prototypes for strcat and strncat. // prototypes for strcat and strncat Append s 2 to s 1. 15 16 cout << "s 1 = " << s 1 << "ns 2 = " << s 2; 17 18 strcat( s 1, s 2 ); 19 20 21 cout << "nn. After strcat(s 1, s 2): ns 1 = " << s 1 Append first 6 characters << "ns 2 = " << s 2; 22 23 24 // concatenate first 6 characters of s 1 to s 3 strncat( s 3, s 1, 6 ); // places '' after last character // concatenate s 2 to s 1 of s 1 to s 3. 25 2003 Prentice Hall, Inc. All rights reserved. 13

26 27 cout << "nn. After strncat(s 3, s 1, 6): ns 1 =

26 27 cout << "nn. After strncat(s 3, s 1, 6): ns 1 = " << s 1 Append s 1 to s 3. << "ns 3 = " << s 3; 28 29 30 31 strcat( s 3, s 1 ); // concatenate s 1 to s 3 cout << "nn. After strcat(s 3, s 1): ns 1 = " << s 1 << "ns 3 = " << s 3 << endl; 32 33 return 0; 34 35 Outline // indicates successful termination } // end main s 1 = Happy s 2 = New Year After strcat(s 1, s 2): s 1 = Happy New Year s 2 = New Year After strncat(s 3, s 1, 6): s 1 = Happy New Year s 3 = Happy After strcat(s 3, s 1): s 1 = Happy New Year s 3 = Happy New Year 2003 Prentice Hall, Inc. All rights reserved. 14

String Manipulation Functions of the String -handling Library • Comparing strings – Characters represented

String Manipulation Functions of the String -handling Library • Comparing strings – Characters represented as numeric codes • Strings compared using numeric codes – Character codes / character sets • ASCII – “American Standard Code for Information Interchange” • EBCDIC – “Extended Binary Coded Decimal Interchange Code” 2003 Prentice Hall, Inc. All rights reserved. 15

String Manipulation Functions of the String -handling Library • Comparing strings – int strcmp(

String Manipulation Functions of the String -handling Library • Comparing strings – int strcmp( const char *s 1, const char *s 2 ) • Compares character by character • Returns – Zero if strings equal – Negative value if first string less than second string – Positive value if first string greater than second string – int strncmp( const char *s 1, const char *s 2, size_t n ) • Compares up to specified number of characters • Stops comparing if reaches null character in one of arguments 2003 Prentice Hall, Inc. All rights reserved. 16

1 2 3 // Fig. 8. 33: fig 08_33. cpp // Using strcmp and

1 2 3 // Fig. 8. 33: fig 08_33. cpp // Using strcmp and strncmp. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 #include <cstring> 13 14 15 16 17 18 19 20 21 22 23 24 25 Outline <cstring> contains prototypes for strcmp and strncmp. // prototypes for strcmp and strncmp int main() { char *s 1 = "Happy New Year"; char *s 2 = "Happy New Year"; char *s 3 = "Happy Holidays"; cout << << << "s 1 = " << s 1 << "ns 2 = " << s 2 Compare s 1 "ns 3 = " << s 3 << "nnstrcmp(s 1, s 2) = " setw( 2 ) << strcmp( s 1, s 2 ) "nstrcmp(s 1, s 3) = " << setw( 2 ) strcmp( s 1, s 3 ) << "nstrcmp(s 3, s 1) = " setw( 2 ) << strcmp( s 3, s 1 ); Compare s 1 and s 2. and s 3. Compare s 3 and s 1. 2003 Prentice Hall, Inc. All rights reserved. 17

Compare up to 6 characters of Compare "nnstrncmp(s 1, s 3, 6) = "

Compare up to 6 characters of Compare "nnstrncmp(s 1, s 3, 6) = " <<s 1 setw( 2 ) up to 7 characters of and s 3. s 1 and s 3. strncmp( s 1, s 3, 6 ) << "nstrncmp(s 1, s 3, 7) = " Compare up to 7 characters of setw( 2 ) << strncmp( s 1, s 3, 7 ) s 3 and s 1. "nstrncmp(s 3, s 1, 7) = " 26 27 28 29 30 31 cout << << << setw( 2 ) << strncmp( s 3, s 1, 7 ) << endl; 32 33 return 0; 34 35 Outline // indicates successful termination } // end main s 1 = Happy New Year s 2 = Happy New Year s 3 = Happy Holidays strcmp(s 1, s 2) = 0 strcmp(s 1, s 3) = 1 strcmp(s 3, s 1) = -1 strncmp(s 1, s 3, 6) = 0 strncmp(s 1, s 3, 7) = 1 strncmp(s 3, s 1, 7) = -1 2003 Prentice Hall, Inc. All rights reserved. 18

String Manipulation Functions of the String -handling Library • Tokenizing – Breaking strings into

String Manipulation Functions of the String -handling Library • Tokenizing – Breaking strings into tokens, separated by delimiting characters – Tokens are usually logical units, such as words (separated by spaces) – "This is my string" has 4 word tokens (separated by spaces) – char *strtok( char *s 1, const char *s 2 ) • Multiple calls required – First call contains two arguments, string to be tokenized and string containing delimiting characters • Finds next delimiting character and replaces with null character – Subsequent calls continue tokenizing • Call with first argument NULL 2003 Prentice Hall, Inc. All rights reserved. 19

1 2 3 // Fig. 8. 34: fig 08_34. cpp // Using strtok. #include

1 2 3 // Fig. 8. 34: fig 08_34. cpp // Using strtok. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 int main() { char sentence[] = "This is a sentence with 7 tokens" ; char *token. Ptr; Outline <cstring> contains prototype for strtok. // prototype for strtok 14 15 16 cout << "The string to be tokenized is: n" << sentence First call to strtok begins << "nn. The tokens are: nn"; 17 18 19 // begin tokenization of sentence token. Ptr = strtok( sentence, " " ); tokenization. 2003 Prentice Hall, Inc. All rights reserved. 20

21 22 23 24 // continue tokenizing sentence until token. Ptr becomes NULL while

21 22 23 24 // continue tokenizing sentence until token. Ptr becomes NULL while ( token. Ptr != NULL ) { cout << token. Ptr << 'n'; token. Ptr = strtok( NULL, " " ); // get next token 25 26 } // end while 27 28 cout << "n. After strtok, sentence = " << sentence << endl; 29 30 return 0; 31 32 // indicates successful Outline Subsequent calls to strtok with NULL as first argument to indicate continuation. termination } // end main 2003 Prentice Hall, Inc. All rights reserved. 21

The string to be tokenized is: This is a sentence with 7 tokens Outline

The string to be tokenized is: This is a sentence with 7 tokens Outline The tokens are: This is a sentence with 7 tokens After strtok, sentence = This 2003 Prentice Hall, Inc. All rights reserved. 22

String Manipulation Functions of the String -handling Library • Determining string lengths – size_t

String Manipulation Functions of the String -handling Library • Determining string lengths – size_t strlen( const char *s ) • Returns number of characters in string – Terminating null character not included in length 2003 Prentice Hall, Inc. All rights reserved. 23

1 2 3 // Fig. 8. 35: fig 08_35. cpp // Using strlen. #include

1 2 3 // Fig. 8. 35: fig 08_35. cpp // Using strlen. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 14 int main() { char *string 1 = "abcdefghijklmnopqrstuvwxyz "; char *string 2 = "four"; char *string 3 = "Boston"; 15 16 17 18 19 20 21 cout << << << 22 23 return 0; 24 25 Outline <cstring> contains prototype for strlen. // prototype for strlen "The length of "" << string 1 "" is " << strlen( string 1 ) "n. The length of "" << string 2 "" is " << strlen( string 2 ) "n. The length of "" << string 3 "" is " << strlen( string 3 ) << endl; Using strlen to determine length of strings. // indicates successful termination } // end main 2003 Prentice Hall, Inc. All rights reserved. 24

The length of "abcdefghijklmnopqrstuvwxyz " is 26 The length of "four" is 4 The

The length of "abcdefghijklmnopqrstuvwxyz " is 26 The length of "four" is 4 The length of "Boston" is 6 Outline 2003 Prentice Hall, Inc. All rights reserved. 25