Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts 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
Dale Roberts 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'
Dale Roberts Character Pointers String constant acts like a character pointer char *pc = “ABCDE”; /* declare a character pointer variable */ Variable AddressValue constant731‘A’ constant732‘B’ constant733‘C’ constant734‘D’ constant735‘E’ constant736‘\0’ pc char s1[] = “abc”; Variable AddressValue s1[0] 900‘a’ s1[1] 901 ‘b’ s1[2] 902 ‘c’ s1[3] 903 ‘\0’ ‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘\0’
Dale Roberts 1100 Example: char s1[] = “abc”; char *s2 = “abc”; f(){ s1[1] = ‘y’; /* OK */ s2[1] = ‘y’; /* wrong (PC is OK)*/ s1 = “test”; /* wrong */ s2 = “test”; /* OK */ } Example: char s3[] = “abcdef”; f1(){ char *s = s3; *s = ‘A’; /* s3[0]=‘A’ */ s = “test”; printf(“%s\n%s\n”,s,s2);} Character Pointers ‘a’ ‘b’ ‘c’ ‘\0’ CONSTANT MEMORY AREA (READ ONLY) ‘t’ ‘e’ ‘s’ ‘t’ ‘\0’ s1[] ‘a’ ‘b’ ‘c’ ‘\0’ s s3[] ‘a’ ‘b’ ‘c’ ‘d’ 2000 s ‘e’ s3[0]=‘A’
Dale Roberts VariableAddressValue constant90‘A’ constant90‘A’ constant91‘B’ constant92‘C’ constant93‘\0’ constant94‘D’ constant95‘E’ constant96‘F’ constant97‘\0’ constant98‘G’ constant99‘H’ Constant 100‘\0’ pc[0] pc[1] pc[2] Pointer Arrays Syntax: int *pi[3]; /* pi[0], pi[1], pi[2] */ float *pf[3]; /* pf[0], pf[1], pf[2] */ Example 1: int i=1, j=2, k=3; int *pi[3] = {&i, &j, &k}; Const can not be changedVariableAddressValue i801 j822 k843 pi[0]10080 pi[1]10182 pi[2]10284 Example 2: char *pc[3]={“ABC”, “DEF”, “GH”};
Dale Roberts argc and argv In environments those support C, there is a way to pass command- line arguments or parameters to a program when it begin executing. When main is called to begin execution, it is called with two arguments – argc and argv argc : The first (conventionally called argc ) is the number of command- line arguments the program was invoked with argv : The second (conventionally called argv ) is a pointer to an array of character strings that contain the arguments, one per string. Example: if echo is a program and executed on phoenix prompt, such as if echo is a program and executed on phoenix prompt, such as 10 echo hello world 10 echo hello world Command-Line Arguments pointer array argv argc e c h o \0 h e l l o \0 w o r l d \0 null 3
Dale Roberts Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; int i; for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’);} main (int argc, char *argv[]) { while (--argc > 0) while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’);} main (int argc, char *argv[]) { while (--argc > 0) while (--argc > 0) printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); printf((argc > 1) ? “%s “ ; “%s\n“, *++argv);}
Dale Roberts Character Handling Library Character handling library Includes functions to perform useful tests and manipulations of character data Each function receives a character (an int ) or EOF as an argument The following slide contains a table of all the functions in The following slide contains a table of all the functions in
Dale Roberts Character Handling Library
Dale Roberts String Conversion Functions Conversion functions In (general utilities library) Convert strings of digits to integer and floating- point values
Dale Roberts 1/* Fig. 8.6: fig08_06.c 2 Using atof */ 3#include 4#include 5 6int main() 7{7{ 8 double d; 9 10 d = atof( "99.0" ); 11 printf( "%s%.3f\n%s%.3f\n", 12 "The string \"99.0\" converted to double is ", d, 13 "The converted value divided by 2 is ", 14 d / 2.0 ); 15 return 0; 16} The string "99.0" converted to double is The converted value divided by 2 is Initialize variable 2. Convert string and assign to variable assign to variable 3. Print Program Output
Dale Roberts Standard Input/Output Library Functions Functions in Functions in Used to manipulate character and string data
Dale Roberts 1. Initialize variables 2. Input 3. Print 3.1 Function definition (note recursion) Program Output 1/* Fig. 8.13: fig08_13.c 2 Using gets and putchar */ 3#include 4 5int main() 6{6{ 7 char sentence[ 80 ]; 8 void reverse( const char * const ); 9 10 printf( "Enter a line of text:\n" ); 11 gets( sentence ); printf( "\nThe line printed backwards is:\n" ); 14 reverse( sentence ); return 0; 17} 18 19void reverse( const char * const sPtr ) 20{ 21 if ( sPtr[ 0 ] == '\0' ) 22 return; 23 else { 24 reverse( &sPtr[ 1 ] ); 25 putchar( sPtr[ 0 ] ); 26 } 27} Enter a line of text: Characters and Strings The line printed backwards is: sgnirtS dna sretcarahC reverse calls itself using substrings of the original string. When it reaches the '\0' character it prints using putchar
Dale Roberts String Manipulation Functions String handling library has functions to Manipulate string data Search strings Tokenize strings Determine string length
Dale Roberts 1. Initialize variables 2. Function calls 3. Print Program Output 1/* Fig. 8.19: fig08_19.c 2 Using strcat and strncat */ 3#include 4#include 5 6int main() 7{7{ 8 char s1[ 20 ] = "Happy "; 9 char s2[] = "New Year "; 10 char s3[ 40 ] = ""; 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} s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year
Dale Roberts 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 int strncmp( const char *s1, const char *s2, size_t n ); size_t n ); Compares up to n characters of string s1 to s2 Returns values as above
Dale Roberts Search Functions of the String Handling Library
Dale Roberts Other Functions of the String Handling Library char *strerror( int errornum ); Creates a system-dependent error message based on errornum Returns a pointer to the string size_t strlen( const char *s ); Returns the number of characters (before NULL ) in string s 1/* Fig. 8.37: fig08_37.c 2 Using strerror */ 3#include 4#include 5 6int main() 7{7{ 8 printf( "%s\n", strerror( 2 ) ); 9 return 0; 10} No such file or directory 1. Function call 2. Print Program Output