0% found this document useful (0 votes)
8 views13 pages

Eee0115 7

Uploaded by

gunetoday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Eee0115 7

Uploaded by

gunetoday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

EEE0115

Chapter 8: C Characters
and Strings

C How to Program
1 Deitel & Deitel
2

Outline
 Fundamentals of Strings and Characters
 Character-Handling Library
 String-Conversion Functions
 Standard Input/Output Library Functions
 String Manipulation Functions
 String Comparison Functions
3

Fundamentals of Strings and Characters


 Characters
 Character constant is an int value
represented as a character
 Strings
 A series of characters considered as a
single unit
 String literal is written in double quotes
"Hello"
 Basically strings are arrays of characters
 The actual value of string is the address of
first character
4

Fundamentals of Strings and Characters


 String definitions
 Define as a character array or a variable
of type char *
char color[] = "blue";
char *colorPtr = "blue";
 Strings represented as character arrays
end with ‘\0’
 color variable has 4+1=5 elements
 To input strings using scanf:
 scanf("%s", word);
5

Character-Handling Library (ctype.h)

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( void )
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
6

Character-Handling Library
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
28 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n",
29 "According to isxdigit:",
30 isxdigit( 'F' ) ? "F is a " : "F is not a ",
31 "hexadecimal digit",
32 isxdigit( 'J' ) ? "J is a " : "J is not a ",
33 "hexadecimal digit",
34 isxdigit( '7' ) ? "7 is a " : "7 is not a ",
35 "hexadecimal digit",
36 isxdigit( '$' ) ? "$ is a " : "$ is not a ",
7

Character-Handling Library
37 "hexadecimal digit",
38 isxdigit( 'f' ) ? "f is a " : "f is not a ",
39 "hexadecimal digit" );
40
41 return 0; /* indicates successful termination */
42
43 } /* end main */

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
8

String-Conversion Functions (stdlib.h)

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

Standard Input/Output Library Functions


(stdio.h)

Function prototype Function description


int getchar( void ); Inputs the next character from the standard input and
returns it as an integer.
char *gets( char *s ); Inputs characters from the standard input into the array
s until a newline or end-of-file character is encountered.
A terminating null character is appended to the array.
Returns the string inputted into s. Note that an error will
occur if s is not large enough to hold the string.
int putchar( int c ); Prints the character stored in c and returns it as an integer.

int puts( const char *s ); Prints the string s followed by a newline character. Returns
a non-zero integer if successful, or EOF if an error occurs.

int sprintf( char *s, const char *format, ... );


Equivalent to printf, except the output is stored in
the array s instead of printed on the screen. Returns
the number of characters written to s, or EOF if an
error occurs.
int sscanf( char *s, const char *format, ... );
Equivalent to scanf, except the input is read from
the array s rather than from the keyboard. Returns the
number of items successfully read by the function, or
EOF if an error occurs.
10

String Manipulation Functions (string.h)

Function prototype Function description


char *strcpy( char *s1, const char *s2 )
Copies string s2 into array s1. The value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n )
Copies at most n characters of string s2 into array s1. The value of
s1 is returned.
char *strcat( char *s1, const char *s2 )
Appends string s2 to array s1. The first character of s2 overwrites
the terminating null character of s1. The value of s1 is returned.

char *strncat( char *s1, const char *s2, size_t n )


Appends at most n characters of string s2 to array s1. The first
character of s2 overwrites the terminating null character of s1.
The value of s1 is returned.
11

String Comparison Functions (string.h)

Function prototype Function description


int strcmp( const char *s1, const char *s2 );
Compares the string s1 with the string s2. The function returns
0, less than 0 or greater than 0 if s1 is equal to, less than or
greater than s2, respectively.
int strncmp( const char *s1, const char *s2, size_t n );
Compares up to n characters of the string s1 with the string s2.
The function returns 0, less than 0 or greater than 0 if s1 is
equal to, less than or greater than s2, respectively.
12

String Comparison Functions (string.h)

1 /* Fig. 8.21: fig08_21.c


2 Using strcmp and strncmp */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 const char *s1 = "Happy New Year"; /* initialize char pointer */
9 const char *s2 = "Happy New Year"; /* initialize char pointer */
10 const char *s3 = "Happy Holidays"; /* initialize char pointer */
11
12 printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n",
13 "s1 = ", s1, "s2 = ", s2, "s3 = ", s3,
14 "strcmp(s1, s2) = ", strcmp( s1, s2 ),
15 "strcmp(s1, s3) = ", strcmp( s1, s3 ),
16 "strcmp(s3, s1) = ", strcmp( s3, s1 ) );
17
13

String Comparison Functions (string.h)


18 printf("%s%2d\n%s%2d\n%s%2d\n",
19 "strncmp(s1, s3, 6) = ", strncmp( s1, s3, 6 ),
20 "strncmp(s1, s3, 7) = ", strncmp( s1, s3, 7 ),
21 "strncmp(s3, s1, 7) = ", strncmp( s3, s1, 7 ) );
22
23 return 0; /* indicates successful termination */
24
25 } /* end main */

s1 = Happy New Year


s2 = Happy New Year
s3 = Happy Holidays

strcmp(s1, s2) = 0
strcmp(s1, s3) = 1
strcmp(s3, s1) = -1

strncmp(s1, s3, 6) = 0
strncmp(s1, s3, 7) = 1
strncmp(s3, s1, 7) = -1

You might also like