Docsity

String and Character Handling in C: Functions and Libraries, Slides of Computer Fundamentals

Toggle sidebar
bg1
Chapter 8 - Characters and Strings
1
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
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download String and Character Handling in C: Functions and Libraries and more Slides Computer Fundamentals in PDF only on Docsity!

Chapter 8 - Characters and Strings

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

Objectives

• In this chapter, you will learn:

– To be able to use the functions of the character handling

library (ctype).

– To be able to use the string and character input/output

functions of the standard input/output library (stdio).

– To be able to use the string conversion functions of the

general utilities library (stdlib).

– To be able to use the string processing functions of the

string handling library (string).

– To appreciate the power of function libraries as a means

of achieving software reusability.

8.2 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

8.2 Fundamentals of Strings and

Characters

  • String definitions
    • Define 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'

8.3 Character Handling Library

Prototype Description

int isdigit( int c ); Returns true if c is a digit and false otherwise.

int isalpha( int c ); Returns true if c is a letter and false otherwise.

int isalnum( int c ); Returns true if c is a digit or a letter and false otherwise.

int isxdigit( int c ); Returns true if c is a hexadecimal digit character and false otherwise.

int islower( int c ); Returns true if c is a lowercase letter and false otherwise.

int isupper( int c ); Returns true if c is an uppercase letter; false otherwise.

int tolower( int c ); If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower

returns the argument unchanged.

int toupper( int c ); If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper

returns the argument unchanged.

int isspace( int c ); Returns true if c is a white-space character—newline ('\n'), space (' '), form feed

('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')—and false

otherwise

int iscntrl( int c ); Returns true if c is a control character and false otherwise.

int ispunct( int c ); Returns true if c is a printing character other than a space, a digit, or a letter and false

otherwise.

int isprint( int c ); Returns true value if c is a printing character including space (' ') and false otherwise.

int isgraph( int c ); Returns true if c is a printing character other than space (' ') and false otherwise.

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

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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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 */

8.4 String Conversion Functions

• Conversion functions

– In <stdlib.h> (general utilities library)

• Convert strings of digits to integer and

floating-point values

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 */

The string "99.0" converted to double is 99.

The converted value divided by 2 is 49.

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 */

The string "1000000" converted to long int is 1000000

The converted value divided by 2 is 500000

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 */

The string "51.2% are admitted" is converted to the

double value 51.20 and the string "% are admitted"