Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.

Similar presentations


Presentation on theme: "CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014."— Presentation transcript:

1 CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014

2 Syllabus String Data Simple String Operations Examples String I/O Functions String Library Functions String to Number Conversion Arrays of Strings

3 2 String Data C++ lacks the built-in type string Strings are approximated in C++ via arrays of characters, i.e. arrays of type char Declaration: char stringname[ SIZE ]; Characters in the string occupy consecutive char elements in the array

4 3 The first '\0' (null) character found in a string literal marks the end of the string. It must be present for literals, inserted by your C++ compiler When declaring a string, the array size should also be long enough to hold both the string and '\0’ Declaring a character array larger than the expected string length allows the array to accommodate a larger string at a later time

5 4 Example: // Declare two strings char str1[ 4 ]; char str2[ 10 ]; // Initialize string one char at a time str1[0]= 'C'; str1[1]= 'a'; str1[2]= 't'; str1[3]= '\0'; // Use up only 5 out of 10 elements str2[0]= 'J'; str2[1]= 'a'; str2[2]= 'n'; str2[3]= 'e'; str2[4]= '\0';

6 5 A string literal, A.K.A. constant is delimited by double quotation marks (e.g., "Hello, world!”) A literal string automatically has a '\0' at the end of the string A string variable can be declared and initialized with a string literal Example: char s[10] = "Hi, Bob!"; char mystr[] = " Good night everybody! ”; char t1[4] = "Jim"; // OK char t2[3] = "Jim"; // ERROR! Overflow

7 6 Example: char s[10] = "Hi, Bob!"; s[0]'H' 1218 s[1]'i' 1219 s[2]',' 1220 s[3]' 1221 s[4]'B' 1222 s[5]'o' 1223 s[6]'b' 1224 s[7]'!' 1225 s[8]'\0' 1226 s[9] 1227 s[0]s[1]s[2]s[3]s[4]s[5]s[6]s[7]s[8]s[9] s → 'H''i'','' 'B''o''b''!' '\0' s is the address of the first character in the string. s[j] is the j-th character in the string. s is equivalent to &s[0]. Declared array size = 10 Valid array index range = 0.. 9

8 7 Example: char s[10] = "Hi, Bob!"; What happens if we do this: s[6] = '\0'; ? The string stored in s is now “Hi, Bo” instead of “Hi, Bob!” s[0]s[1]s[2]s[3]s[4]s[5]s[6]s[7]s[8]s[9] s → 'H''i'','' 'B''o''b''!' '\0' s[0]s[1]s[2]s[3]s[4]s[5]s[6]s[7]s[8]s[9] s → 'H''i'','' 'B''o' '\0' '!' '\0'

9 8 To embed a quote " in a string, use \" Example: char str1[] = "Hello";/* Hello */ char str2[] = "\"Hello\"";/* "Hello" */ To embed a backslash in a string, use \\ Example: char winpath[]="C:\\Windows”; // C:\Windows

10 9 Examples of Simple String Operations Assume these string declarations: char str1[ MAXSIZE ], str2[ MAXSIZE ]; Input a string (requires stdio.h ) fgets( str1, MAXSIZE, stdin ); Print a string printf( "str1 = %s\n", str1 ); Copy a string (requires string.h ) strcpy( str1, "Hello” ); strcpy( str2, str1 ); Copies literal “Hello” to str1.Copies contents of str1 to str2.Stores input in str1.

11 10 Example: /* Simple string operations demonstration */ #include #define MAXLEN 100 int main( void ) { // main char str1[ MAXLEN ], str2[ MAXLEN ]; printf( "What is your name? ” ); fgets( str1, MAXLEN, stdin ); printf( "Hello, %s!\n", str1 ); return 0; } //end main Output: What is your name? Joe Hello, Joe ! Why does the exclamation point show up on a separate line? fgets() stores the newline ( '\n' ) that you typed as part of the string itself.

12 11 String I/O Functions C has library functions to input and output strings Use #include In the following table, char * is a pointer to a character array String I/O Functions int printf( const char * fmt, … ) Prints formatted output to console int sprintf( char * s, const char * fmt, … ) Prints formatted output to string s int puts( const char * s ) Prints string to console int scanf( const char * fmt, … ) Reads formatted input from console int fgets( const char * s, FILE * stream ) Reads string from input stream

13 12 printf() int printf( const char * format, …); Writes formatted output to the console:  Use the %s format specifier for strings  %s expects the address of a char array which contains a string Example: char name[] = "Jane Doe"; printf("[%s]\n", name ); → [Jane Doe] printf("[%s]\n", & name[0] ); → [Jane Doe] printf("[%s]\n", & name[5] ); → [Doe]

14 13 sprintf() int sprintf( char * s, const char * format, …); Performs a formatted print and saves it into a string  Works like printf except the output is stored in string s  Nothing is printed to the console Example: char str[50]; int x = 4; sprintf( str, "x=%d y=%f", x, 1.5f ); printf( "title = %s\n", str ); title = x=4 y=1.500000 <- and line feed

15 14 puts() int puts( const char * s ); Writes a string to the console  A newline '\n' is automatically printed at the end of the string Example: char name[] = "Jane Doe"; puts( name ); printf( "lives here.\n” ); Output: Jane Doe lives here.

16 15 scanf() int scanf( const char * format, …); Reads formatted input from the console  Use the %s format specifier for strings  %s expects the address of a char array  Only reads single “words” at a time (whitespace delimited) Example: char str[100];/* String storage */ scanf( "%s", str );/* str is address */ scanf( "%s", &str[0] ); // synonymous to str

17 16 scanf()  scanf skips whitespace( space, tab, newline) and then stores in str all characters up to the next whitespace  scanf automatically adds '\0' to the end of the array  It does not check for char array overflow. The array must be large enough to hold the expected string and the '\0’  Any unused text remains in the input stream Example: scanf( "%s", str1 ); User types in: Hello, Portland !!! scanf( "%s", str2 ); → str1 holds Hello, → str2 holds Portland// not the !!!

18 17 fgets() char * fgets( char * s, int m, FILE * stream ); Reads a string from the input stream  For input from the console, use stdin for the stream  fgets reads input characters until one of these conditions is met: m – 1 characters are read '\n' (newline) is read, will be stored if fits into space End-of-file is detected  If successful, returns pointer to s  If end-of-file, returns NULL pointer

19 18 Example #define MAXLEN 10000 char str[ MAXLEN ]; // String storage if( fgets( str, MAXLEN, stdin ) ) { printf( "%s", str ); }else{ printf( "End of file\n” ); } //end if

20 19  If a newline is entered to signal the end of input, the '\n' character is also stored as part of string  fgets automatically terminates the string with '\0’ Example: fgets( str, 10, stdin ); Suppose the user types in: Jim May  printf( "[%s]", str ); → [Jim May ] str → 'J''i''m'' 'M''a''y''\n''\0' User types Enter key (newline)

21 20 Is there a simple way to get rid of the extraneous '\n' when using fgets ? This is one approach that works: fgets( s, MAXLEN, stdin ); if( strchr( s,'\n') != NULL ) s[ strlen(s) - 1 ] = '\0'; Caveat: The if check is necessary because a Ctrl-D terminates input without adding an extra '\n’ Warning:There is also a function; gets(). DO NOT USE IT!! gets() does not check for char array overflow

22 21 String Library Functions C has a library of string processing functions Use #include In the following table: s is of type char * (pointer to char array) n is of type size_t (unsigned integer) cs and ct are of type const char * c is an int converted to char

23 22 Some Common string lib functions size_t strlen( cs ) return length of cs. char * strcpy( s, ct ) copy string ct to string s, including ‘\0’; return s. char * strncpy( s, ct, n ) copy at most n characters of string ct to s ; return s. Pad with ‘\0’s if ct has fewer than n characters. char * strcat( s, ct ) concatenate string ct to end of string s ; return s. char * strncat( s, ct, n ) concatenate at most n characters of string ct to string s, terminate s with ‘\0’; return s. int strcmp( cs, ct ) compare string cs to string ct ; return 0 if cs > ct. int strncmp( cs, ct, n ) compare at most n characters of string cs to string ct ; return 0 if cs > ct. char * strchr( cs, c ) return pointer to first occurrence of c in cs or NULL if not present char * strrchr( cs, c ) return pointer to last occurrence of c in cs or NULL if not present char * strstr( cs, ct ) return pointer to first occurrence of string ct in cs, or NULL if not present

24 23 strlen() size_t strlen( const char * s ); Determines the length of the string s.  The function counts the number of characters in the array.  The count starts at array index 0.  It continues counting until the first '\0' is found. ('\0' itself is not included in the string length.) Example: strlen("") → 0 char x[] = "Cat"; strlen(x) → 3 SL = strlen(" PSU Vikings") → 12

25 24 strcpy() char * strcpy( char * d, const char * s ); Copies contents of string s to string d  The terminating '\0' is also copied to d.  The contents of d are overwritten.  The address of string d is returned.  Strings cannot be copied using the = assignment operator. Use strcpy() instead. Example: char dst[20], src[] = "Hello"; strcpy( dst, "Siri” ); → dst holds Siri strcpy( dst, src ); → dst holds Hello

26 25 strcat() char * strcat( char * d, const char * s ); Concatenates contents of string s to string d  The contents of s are appended to the end of whatever is already in d  The original '\0' in d is deleted before appending happens  The address of the new string d is returned  Strings cannot be concatenated using the + operator in C. Use strcat() instead. Example: char dst[20] = "PSU", src[] = " rocks!"; strcat( dst, src ); → dst holds PSU rocks!

27 26 strcmp() int strcmp( const char * s, const char * t ); Compares contents of string s to string t  s and t are compared character by character  Returns zero (0) if the strings are identical positive number if s is lexically greater than t negative number if t is lexically greater than s  Strings cannot be compared using a relational operator. Use strcmp() instead Example: char s1[5] = "PSU", s2[10] = "OSU"; strcmp( s1, s2 ); → returns 1 strcmp( s2, s1 ); → returns -1

28 27 Example: char a[10], b[10];/* Two strings */ strcpy( a, "Star” );/* a: Star */ strcpy( b, a );/* b: Star */ strcat( a, " Trek” );/* a: Star Trek */ strcat( b, " Wars” );/* b: Star Wars */ if( strlen(a) > 0 && strlen(b) > 0 ) if( strcmp( a, b ) == 0 ) printf( "You're kidding, right?\n” ); else if( strcmp( a, b ) < 0 ) printf( "Trekker!\n” );

29 28 strchr() char * strchr( const char * s, int c ); Finds position of character c within string s.  If c is found, returns pointer to first occurrence of c in s. If c is not found, returns NULL pointer. Example: char s[] = "PSU OSU"; strchr( s, 'A’ ); → returns NULL p = strchr( s, 'U’ ); → returns pointer to first U

30 29 strchr() char * strstr( const char * s, const char * t ); Finds position of string t within string s.  If t is found, returns pointer to first occurrence of t in s. If t is not found, returns NULL pointer. Example: char s[] = "PSU OSU"; strstr( s, "A" ); → returns NULL p = strstr( s, "U O" ); → returns ptr to location

31 30 String to Number Conversion Use #include Conversion functions (partial list) Example: double x; char numstr[] = "12.75 HI"; x = atof( numstr ); → x contains 12.75 x = atof( "HI 12.75” ); → x contains 0.0 printf( "%d\n", atoi( numstr ) ); → displays 12 double atof( const char * s ) Converts s to a number of type double int atoi( const char * s ) Converts s to a number of type int long atol( const char * s ) Converts s to a number of type long int

32 31 Array of Strings An array of strings (“string array”) is a 2-D array Each row represents a separate string When declaring the array, the number of columns must be large enough to hold the largest expected string An individual string within the array can be accessed by using just the row index

33 32 Example: /* 4 strings of up to 10 chars each */ char s[4][10]; strcpy( s[0], "Doe, Jane” ); strcpy( s[3], "Rand, Bob” ); /* Assume user enters: Li, Joe  */ fgets( s[1], 10, stdin ); /* Assume user enters: Li, Joe  */ scanf( "%s", s[2] ); printf( "%s\n", s[0] ); /* Doe, Jane */ printf( "%s\n", &s[0][5] ); /* Jane */ printf( "%c\n", s[3][6] ); /* B */

34 33 Example: (continued from previous page) s is the name for the entire array of strings. s[i] is the address of the i-th string in the array. s[i][j] is j-th character of the i-th string. s[i] is equivalent to &s[i][0]. s[3] → 'R''a''n''d'','' 'B'o''b''\0' length = 9 s[2] → 'L''i'',''\0' length = 3 s[1] → 'L''i'','' 'J''o''e''\n''\0' length = 8 0123456789 s[0] → 'D''o''e'','' 'J''a''n''e''\0' length = 9

35 34 Example: Write a string length function using arrays #include /* Function returns the length of a string */ int str_len( const char s[]) { // str_len int k = 0; /* Array index for string */ while( s[k] != '\0') /* Look for string terminator */ k++; return k; /* k is also the length */ } //end str_len int main( void ) { // main char a[] = "Hello!"; printf( "String length is %d.\n", str_len(a) ); return 0; } //end main

36 35 Example: Write a string copy function using arrays /* String copy: source: cs thus can be const, destination: st */ char * str_cpy( char st[], const char cs[] ) { int k = 0; // Array index for string while( cs[k] != '\0’ ) { /* Look for string terminator */ st[k] = cs[k]; /* Copy single character at a time */ k++; } //end while st[k] = '\0'; /* Add string terminator to destination */ return st; // st by itself is an address } //end str_cpy


Download ppt "CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014."

Similar presentations


Ads by Google