1 / 27

ECE 103 Engineering Programming Chapter 29 C Strings, Part 2

ECE 103 Engineering Programming Chapter 29 C Strings, Part 2. Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE. Syllabus. String I/O Functions String Library Functions String to Number Conversion

levi
Download Presentation

ECE 103 Engineering Programming Chapter 29 C Strings, Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 103 Engineering ProgrammingChapter 29C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

  2. Syllabus • String I/O Functions • String Library Functions • String to Number Conversion • Arrays of Strings

  3. String I/O Functions C has library functions to input and output strings Use #include <stdio.h> In the following table, char * is a pointer to a character array --strictly speaking, to a char-- that contains the string String I/O Functions 2

  4. 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] 3

  5. 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 ); 4

  6. 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. 5

  7. 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 6

  8. 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 7

  9. 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 End-of-file is detected If successful, returns pointer to s If end-of-file, returns NULL pointer 8

  10. Example #define MAXLEN 80 char str[ MAXLEN ]; /* String storage */ if( fgets( str, MAXLEN, stdin ) != NULL ) { printf( "%s", str ); }else{ printf( "End of file\n” ); } //end if 9

  11. 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 ] User types Enter key (newline) 10

  12. 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 11

  13. String Library Functions C has a library of string processing functions. Use #include <string.h> 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 12

  14. Some Common string lib functions 13

  15. 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 14

  16. 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 15

  17. 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 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! 16

  18. 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 17

  19. 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” ); 18

  20. 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 19

  21. 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 20

  22. String to Number Conversion Use #include <stdlib.h> 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 21

  23. 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 22

  24. 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 */ 23

  25. Example:(continued from previous page) length = 9 length = 8 length = 3 length = 9 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]. 24

  26. Example: Write a string length function using arrays #include <stdio.h> /* 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 25

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

More Related