












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Outline
8.1 Introduction
8.2 Fundamentals of Strings and Characters
8.3 Character Handling Library
8.4 String Conversion Functions
8.5 Standard Input/Output Library Functions
8.6 String Manipulation Functions of the String Handling
Library
8.7 Comparison Functions of the String Handling Library
8.8 Search Functions of the String Handling Library
8.9 Memory Functions of the String Handling Library
8.10 Other Functions of the String Handling Library
8.2 Fundamentals of Strings and
Characters
8.2 Fundamentals of Strings and
Characters
Prototype Description
fig08_02.c (Part 1 of 2)
1 /* Fig. 8.2: fig08_02.c 2 Using functions isdigit, isalpha, isalnum, and isxdigit */ 3 #include <stdio.h> 4 #include <ctype.h> 5 6 int main() 7 { 8 printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ", 9 isdigit( '8' )? "8 is a " : "8 is not a ", "digit", 10 isdigit( '#' )? "# is a " : "# is not a ", "digit" ); 11 12 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 13 "According to isalpha:", 14 isalpha( 'A' )? "A is a " : "A is not a ", "letter", 15 isalpha( 'b' )? "b is a " : "b is not a ", "letter", 16 isalpha( '&' )? "& is a " : "& is not a ", "letter", 17 isalpha( '4' )? "4 is a " : "4 is not a ", "letter" ); 18 19 printf( "%s\n%s%s\n%s%s\n%s%s\n\n", 20 "According to isalnum:", 21 isalnum( 'A' )? "A is a " : "A is not a ", 22 "digit or a letter", 23 isalnum( '8' )? "8 is a " : "8 is not a ", 24 "digit or a letter", 25 isalnum( '#' )? "# is a " : "# is not a ", 26 "digit or a letter" ); 27
Program Output
fig08_03.c (Part 1 of 2)
1 /* Fig. 8.3: fig08_03.c 2 Using functions islower, isupper, tolower, toupper */ 3 #include <stdio.h> 4 #include <ctype.h> 5 6 int main() 7 { 8 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 9 "According to islower:", 10 islower( 'p' )? "p is a " : "p is not a ", 11 "lowercase letter", 12 islower( 'P' )? "P is a " : "P is not a ", 13 "lowercase letter", 14 islower( '5' )? "5 is a " : "5 is not a ", 15 "lowercase letter", 16 islower( '!' )? "! is a " : "! is not a ", 17 "lowercase letter" ); 18 19 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 20 "According to isupper:", 21 isupper( 'D' )? "D is an " : "D is not an ", 22 "uppercase letter", 23 isupper( 'd' )? "d is an " : "d is not an ", 24 "uppercase letter", 25 isupper( '8' )? "8 is an " : "8 is not an ", 26 "uppercase letter", 27 isupper( '$' )? "$ is an " : "$ is not an ", 28 "uppercase letter" ); 29
fig08_04.c (Part 1 of 2)
1 /* Fig. 8.4: fig08_04.c 2 Using functions isspace, iscntrl, ispunct, isprint, isgraph */ 3 #include <stdio.h> 4 #include <ctype.h> 5 6 int main() 7 { 8 printf( "%s\n%s%s%s\n%s%s%s\n%s%s\n\n", 9 "According to isspace:", 10 "Newline", isspace( '\n' )? " is a " : " is not a ", 11 "whitespace character", "Horizontal tab", 12 isspace( '\t' )? " is a " : " is not a ", 13 "whitespace character", 14 isspace( '%' )? "% is a " : "% is not a ", 15 "whitespace character" ); 16 17 printf( "%s\n%s%s%s\n%s%s\n\n", "According to iscntrl:", 18 "Newline", iscntrl( '\n' )? " is a " : " is not a ", 19 "control character", iscntrl( '$' )? "$ is a " : 20 "$ is not a ", "control character" ); 21
fig08_04.c (Part 2 of 2)
22 printf( "%s\n%s%s\n%s%s\n%s%s\n\n", 23 "According to ispunct:", 24 ispunct( ';' )? "; is a " : "; is not a ", 25 "punctuation character", 26 ispunct( 'Y' )? "Y is a " : "Y is not a ", 27 "punctuation character", 28 ispunct( '#' )? "# is a " : "# is not a ", 29 "punctuation character" ); 30 31 printf( "%s\n%s%s\n%s%s%s\n\n", "According to isprint:", 32 isprint( '$' )? "$ is a " : "$ is not a ", 33 "printing character", 34 "Alert", isprint( '\a' )? " is a " : " is not a ", 35 "printing character" ); 36 37 printf( "%s\n%s%s\n%s%s%s\n", "According to isgraph:", 38 isgraph( 'Q' )? "Q is a " : "Q is not a ", 39 "printing character other than a space", 40 "Space", isgraph( ' ' )? " is a " : " is not a ", 41 "printing character other than a space" ); 42 43 return 0 ; /* indicates successful termination / 44 45 } / end main */
Function prototype Function description double atof( const char *nPtr ); (^) Converts the string nPtr to double. int atoi( const char *nPtr ); (^) Converts the string nPtr to int. long atol( const char *nPtr ); (^) Converts the string nPtr to long int. double strtod( const char *nPtr, char **endPtr ); Converts the string^ nPtr^ to^ double. long strtol( const char *nPtr, char **endPtr, int base ); Converts the string^ nPtr^ to^ long. unsigned long strtoul( const char *nPtr, char **endPtr, int base ); Converts the string^ nPtr^ to^ unsigned^ long.
fig 08_06.c
Program Output
1 /* Fig. 8.6: fig08_06.c 2 Using atof / 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 double d; / variable to hold converted string / 9 10 d = atof( "99.0" ); 11 12 printf( "%s%.3f\n%s%.3f\n", 13 "The string "99.0" converted to double is ", d, 14 "The converted value divided by 2 is ", 15 d / 2.0 ); 16 17 return 0 ; / indicates successful termination / 18 19 } / end main */
fig08_08.c
Program Output
1 /* Fig. 8.8: fig08_08.c 2 Using atol / 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 long l; / variable to hold converted string / 9 10 l = atol( "1000000" ); 11 12 printf( "%s%ld\n%s%ld\n", 13 "The string "1000000" converted to long int is ", l, 14 "The converted value divided by 2 is ", l / 2 ); 15 16 return 0 ; / indicates successful termination / 17 18 } / end main */
fig08_09.c
Program Output
1 /* Fig. 8.9: fig08_09.c 2 Using strtod / 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 / initialize string pointer */ 9 const char string = "51.2% are admitted"; 10 11 double d; / variable to hold converted sequence */ 12 char stringPtr; / create char pointer / 13 14 d = strtod( string, &stringPtr ); 15 16 printf( "The string "%s" is converted to the\n", string ); 17 printf( "double value %.2f and the string "%s"\n", d, stringPtr ); 18 19 return 0 ; / indicates successful termination / 20 21 } / end main */