C Chap08
C Chap08
Outline
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
8.9
8.10
Introduction
Fundamentals of Strings and Characters
Character Handling Library
String Conversion Functions
Standard Input/Output Library Functions
String Manipulation Functions of the String Handling
Library
Comparison Functions of the String Handling Library
Search Functions of the String Handling Library
Memory Functions of the String Handling Library
Other Functions of the String Handling Library
8.1
Introduction
Word processors
Page layout software
Typesetting programs
8.2
8.2
Strings
Series of characters treated as a single unit
Can include letters, digits, and certain special characters
(*, /, $)
8.2
String declarations
Declare as a character array or a variable of type char *
char color[] = "blue";
char *colorPtr = "blue";
8.2
Inputting strings
Use scanf
scanf("%s", word);
Copies input into word[], which does not need & (because a
string is a pointer)
8.3
In <ctype.h>
Prototype
Description
Returns true if c is a white-space characternewline ('\n'), space (' '), form feed
('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')and
false otherwise
Returns true if c is a printing character other than a space, a digit, or a letter and false
otherwise.
Returns true value if c is a printing character including space (' ') and false
otherwise.
Returns true if c is a printing character other than space (' ') and false otherwise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Outline
#include <stdio.h>
#include <ctype.h>
int main()
{
printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ",
isdigit( '8' ) ? "8 is a " : "8 is not a ", "digit",
isdigit( '#' ) ? "# is a " :
"# is not a ", "digit" );
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isalpha:",
isalpha( 'A' ) ? "A is a " : "A is not a ", "letter",
isalpha( 'b' ) ? "b is a " : "b is not a ", "letter",
isalpha( '&' ) ? "& is a " : "& is not a ", "letter",
isalpha( '4' ) ? "4 is a " :
"4 is not a ", "letter" );
printf( "%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isalnum:",
isalnum( 'A' ) ? "A is a " : "A is not a ",
"digit or a letter",
isalnum( '8' ) ? "8 is a " : "8 is not a ",
"digit or a letter",
isalnum( '#' ) ? "# is a " : "# is not a ",
"digit or a letter" );
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n",
"According to isxdigit:",
isxdigit( 'F' ) ? "F is a " : "F is not a ",
"hexadecimal digit",
isxdigit( 'J' ) ? "J is a " : "J is not a ",
2000 Prentice
Hall, Inc. Alldigit",
rights reserved.
"hexadecimal
1. Load header
2. Perform tests
3. Print
33
34
"hexadecimal digit",
35
36
"hexadecimal digit",
37
38
"hexadecimal digit" );
39
Outline
return 0;
40 }
According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit
2000 Prentice Hall, Inc. All rights reserved.
Program Output
Description
Outline
Using atof */
#include <stdio.h>
#include <stdlib.h>
5
6
int main()
double d;
9
10
d = atof( "99.0" );
11
printf( "%s%.3f\n%s%.3f\n",
12
13
14
d / 2.0 );
15
1. Initialize variable
2. Convert string
2.1 Assign to variable
3. Print
return 0;
16 }
Program Output
8.5
Functions in <stdio.h>
Function description
Inputsthenextcharacterfromthe
standardinputandreturnsitasaninteger.
Inputscharactersfromthestandardinput
intothearraysuntilanewlineorendof
filecharacterisencountered.A
terminatingnull characterisappendedto
thearray.
Printsthecharacterstoredinc.
Printsthestringsfollowedbyanewline
character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
1. Initialize variables
2. Input
3. Print
3.1 Function definition
(note recursion)
}
void reverse( const char * const sPtr )
{
if ( sPtr[ 0 ] == '\0' )
reverse calls itself using substrings of the
return;
original string. When it reaches the '\0'
else {
character it prints using putchar
reverse( &sPtr[ 1 ] );
putchar( sPtr[ 0 ] );
}
}
8.6
Function description
Outline
#include <stdio.h>
#include <string.h>
5
6
int main()
10
11
12
13
14
15
16
return 0;
1. Initialize variables
2. Function calls
3. Print
17 }
s1 = Happy
s2 = New Year
strcat( s1, s2 ) = Happy New Year
strncat( s3, s1, 6 ) = Happy
strcat( s3, s1 ) = Happy Happy New Year
Program Output
8.7
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 (s1 < s2), zero (s1 == s2), or a
positive number (s1 > s2)
8.8
Function prototype
Function description
Determines and returns the length of the initial segment of string s1 consisting of
characters not contained in string s2.
Determines and returns the length of the initial segment of string s1 consisting only
of characters contained in string s2.
Locates the first occurrence in string s1 of any character in string s2. If a character
from string s2 is found, a pointer to the character in string s1 is returned. Otherwise, a NULL pointer is returned.
char *strrchr( const char *s, Locates the last occurrence of c in string s. If c is found, a pointer to c in string s is
int c );
returned. Otherwise, a NULL pointer is returned.
char *strstr( const char *s1, Locates the first occurrence in string s1 of string s2. If the string is found, a pointer
const char *s2 );
to the string in s1 is returned. Otherwise, a NULL pointer is returned.
char *strtok( char *s1, const A sequence of calls to strtok breaks string s1 into tokenslogical pieces such
char *s2 );
as words in a line of textseparated by characters contained in string s2. The first
call contains s1 as the first argument, and subsequent calls to continue tokenizing
the same string contain NULL as the first argument. A pointer to the current token is
returned by each call. If there are no more tokens when the function is called, NULL
is returned.
Using strspn */
#include <stdio.h>
#include <string.h>
5
6
int main()
10
11
Outline
1. Initialize variables
2. Function calls
3. Print
printf( "%s%s\n%s%s\n\n%s\n%s%u\n",
12
13
14
15
16
return 0;
17 }
Program Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
1. Initialize variables
2. Function calls
3. Print
Program Output
Description
void *memchr(const void *s, int c, size_t Locates the first occurrence of c (converted to unsigned char) in
n )
the first n characters of the object pointed to by s. If c is found, a
pointer to c in the object is returned. Otherwise, 0 is returned.
void *memset( void *s, int c, size_t n ) Copies c (converted to unsigned char) into the first n characters
of the object pointed to by s. A pointer to the result is returned.
Outline
Using memmove */
#include <stdio.h>
#include <string.h>
5
6
int main()
1. Initialize variables
2. Function calls
9
10
printf( "%s%s\n",
11
12
3. Print
printf( "%s%s\n",
13
14
memmove( x, &x[ 5 ], 10 ) );
",
15
16
return 0;
17 }
The string in array x before memmove is: Home Sweet Home
The string in array x after memmove is: Sweet Home Home
Program Output
Using strerror */
Outline
#include <stdio.h>
#include <string.h>
1. Function call
int main()
2. Print
return 0;
10 }
Program Output