100 likes | 263 Views
Department of Computer and Information Science, School of Science, IUPUI. C-Style Strings Strings and String Functions. Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu. f i r s t . Character Arrays. Character arrays String is really a static array of characters, ex: “first”
E N D
Department of Computer and Information Science,School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu
f i r s t \0 Character Arrays • Character arrays • String is really a static array of characters, ex: “first” • Character arrays can be initialized using string literals char string1[] = "first"; • Null character '\0' terminates strings • string1 actually has 6 elements It is equivalent to charstring1[]={'f','i','r','s','t','\0'}; • Can access individual characters string1[3] is character ‘s’ • Array name is address of array, so & not needed for scanf char char string2[20]; scanf("%s",string2); • Can read a string with max of size 19 and a null character. • Reads characters until whitespace (space, tab, carriage-return, newline, vertical tab) encountered • Can write beyond end of array, be careful Null character (indicates string termination) & is NOT used, why?
Fundamentals of Strings and Characters • Characters • Building blocks of programs • Every program is a sequence of meaningfully grouped characters • Character constant • An int value represented as a character in single quotes • 'z' represents the integer value of z • Strings • Series of characters treated as a single unit • Can include letters, digits and special characters (*, /, $) • String literal (string constant) - written in double quotes • "Hello" • Strings are arrays of characters • String a pointer to first character • Value of string is the address of first character
Fundamentals of Strings and Characters • String declarations • Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; • Remember that strings represented as character arrays end with '\0' • color has 5 elements • Inputting strings • Use scanf scanf("%s", word); • Copies input into word[] • Do not need & (because a string is a pointer) • Remember to leave room in the array for '\0'
Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); } main (int argc, char *argv[]) { while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); } main (int argc, char *argv[]) { while (--argc > 0) printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); }
String Manipulation Functions • String handling library has functions to • Manipulate string data • Search strings • Tokenize strings • Determine string length
1 /* Fig. 8.19: fig08_19.c 2 Using strcat and strncat */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 char s1[ 20 ] = "Happy "; 9 char s2[] = "New Year "; 10 char s3[ 40 ] = ""; 11 12 printf( "s1 = %s\ns2 = %s\n", s1, s2 ); 13 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) ); 14 printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) ); 15 printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) ); 16 return 0; 17 } 1. Initialize variables 2. Function calls 3. Print Program Output s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year
1 /* Fig. 8.37: fig08_37.c 2 Using strerror */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 printf( "%s\n", strerror( 2 ) ); 9 return 0; 10 } Other Functions of the String Handling Library char *strerror( interrornum ); • Creates a system-dependent error message based on errornum • Returns a pointer to the string size_tstrlen( const char *s ); • Returns the number of characters (before NULL) in string s 1. Function call 2. Print Program Output No such file or directory
Comparing Strings • A useful operation is the comparison of two strings. Two strings are related in the same three basic ways as number values. One string is either less than, equal to, or greater than the other. String comparison is usually based on the positions of the characters in the character set. • Scanning along both strings and comparing corresponding characters establish the relationship between two strings. The strings are equal as long as corresponding characters are equal. If two characters are different, the comparisons are based on their relative order in the character set. The character whose code is less belongs to the lesser string. • Ex. “abcd” < “abcz” • If the two strings are of different length, but identical up to the end of the shorter one, then the shorter string is the lesser of the two: • Ex. “abc” < “abcd” • If the two strings are of different length and consist of Upper and lowercase letters, Upper case letters come before lower case letter and a blank has a lower value than all other letters. • Ex. “AZZZ” < “Aaaah” • Below is an example of a comparison of strings that contain blanks. Scanning along both strings and comparing corresponding characters, you see the strings are equal for the first two characters. You then compare the blank and the t; you then reach the conclusion below. • Ex. “hi there” < “hit a ball”
Comparison Functions • Comparing strings • Computer compares numeric ASCII codes of characters in string • Appendix D has a list of character codes int strcmp( const char *s1, const char *s2 ); • Compares string s1 to s2 • Returns a negative number if s1 < s2, zero if s1 == s2 or a positive number if s1 > s2 int strncmp( const char *s1, const char *s2, size_t n ); • Compares up to n characters of string s1 to s2 • Returns values as above