c_chap08.ppt

Upload: srinidhi-upadhya

Post on 07-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 C_chap08.ppt

    1/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    Chapter 8 - Characters and Strings

    Outline8.1 Introduction8.2 Fundamentals of Strings and Characters8.3 Character Handling Library8.4 String Conversion Functions8.5 Standard Input/Output Library Functions8.6 String Manipulation Functions of the String Handling

    Library8.7 Comparison Functions of the String Handling Library8.8 Search Functions of the String Handling Library8.9 Memory Functions of the String Handling Library8.10 Other Functions of the String Handling Library

  • 8/19/2019 C_chap08.ppt

    2/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    8.1 Introduction

    • Introduce some standard lirar! "unctions #  $as! string and character processing

     #  Programs can process characters, strings, lines o" te%t, and loc&s

    o" memor!

    • 'hese techni(ues used to ma&e #  )ord processors

     #  Page la!out so"t*are

     #  '!pesetting programs

  • 8/19/2019 C_chap08.ppt

    3/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    8.2 Fundamentals of Strings andCharacters

    • Characters

     #  +uilding loc&s o" programs

    • $ver! program is a se(uence o" meaning"ull! grouped

    characters

     #  Character constant - an int value represented as a character

    in single (uotes• 'z' represents the integer value o" z

  • 8/19/2019 C_chap08.ppt

    4/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    8.2 Fundamentals of Strings andCharacters (II)

    • Strings

     #  Series o" characters treated as a single unit

    • Can include letters, digits, and certain special characters

    *, /, $

     #  String literal string constant - *ritten in doule (uotes

    • "Hello"

     #  Strings are arra!s o" characters

    • String a pointer to "irst character 

    • alue o" string is the address o" "irst character 

  • 8/19/2019 C_chap08.ppt

    5/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    8.2 Fundamentals of Strings andCharacters (III)

    • String declarations #  /eclare as a character arra! or a variale o" t!pe char *

    char color[] = "blue";

    char *colorPtr = "blue";

     #  ememer that strings represented as character arra!s end*ith '\0'• color has 1 elements

  • 8/19/2019 C_chap08.ppt

    6/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    8.2 Fundamentals of Strings andCharacters (IV)

    • Inputting strings #  se scanf

    scanf("%s" !or#;

    • Copies input into !or[], *hich does not need  ecause astring is a pointer

     #  ememer to leave space "or '\0'

  • 8/19/2019 C_chap08.ppt

    7/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.3 Character Handling Library

    • Character Handling 3irar!

     #  Includes "unctions to per"orm use"ul tests and manipulations

    o" character data

     #  $ach "unction receives a character an int) or & as anargument

  • 8/19/2019 C_chap08.ppt

    8/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.3 Character Handling Library (II)

    • In ct)e+h, 

    Protot!pe /escription

    int is"i-it( int c #  0eturns true i" c is a digit and false other*ise.

    int isalha( int c #  0eturns true i" c is a letter and false other*ise.

    int isalnu.( int c #  0eturns true i" c is a digit or a letter and false other*ise.

    int is/"i-it( int c #  0eturns true i" c is a he%adecimal digit character and false other*ise.

    int islo!er( int c #  0eturns true i" c is a lo*ercase letter and false other*ise.

    int isuer( int c # 

    0eturns true i" c is an uppercase letter4 false other*ise.int tolo!er( int c #  I" c is an uppercase letter, tolo!er returns c as a lo*ercase letter. 5ther*ise, tolo!er 

    returns the argument unchanged.

    int touer( int c #  I" c is a lo*ercase letter, touer returns c as an uppercase letter. 5ther*ise, touer returns the argument unchanged.

    int issace( int c #  0eturns true i" c is a *hite-space character6ne*line '\n', space ' ', "orm "eed'\f', carriage return '\r', hori7ontal ta '\t', or vertical ta '\'6and

    false other*iseint iscntrl( int c #  0eturns true i" c is a control character and false other*ise.

    int isunct( int c #  0eturns true i" c is a printing character other than a space, a digit, or a letter and false other*ise.

    int isrint( int c #  0eturns true value i" c is a printing character including space ' ' and false other*ise.

    int is-rah( int c #  0eturns true i" c is a printing character other than space ' ' and false other*ise.

  • 8/19/2019 C_chap08.ppt

    9/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Load header

    2. Perform tests

    3. Print

    1 /* i-+ 2+34 fi-02503+c

    3   6sin- functions isi-it isalha isalnu. an isi-it */

    7 8inclue stio+h, 

    9 8inclue ct)e+h, 

    :

    int .ain(#

    <

    2   rintf( "%s\n%s%s\n%s%s\n\n" ">ccorin- to isi-it4 "

    ?   isi-it( '2' # @ "2 is a " 4 "2 is not a " "i-it"

    10   isi-it( '8' # @ "8 is a " 4

    11   "8 is not a " "i-it" #;

    13   rintf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n"

    17   ">ccorin- to isalha4"

    19   isalha( '>' # @ "> is a " 4 "> is not a " "letter"

    1:   isalha( 'b' # @ "b is a " 4 "b is not a " "letter"1   isalha( '' # @ " is a " 4 " is not a " "letter"

    1<   isalha( '9' # @ "9 is a " 4

    12   "9 is not a " "letter" #;

    1?   rintf( "%s\n%s%s\n%s%s\n%s%s\n\n"

    30   ">ccorin- to isalnu.4"

    31   isalnu.( '>' # @ "> is a " 4 "> is not a "

    33   "i-it or a letter"

    37   isalnu.( '2' # @ "2 is a " 4 "2 is not a "

    39   "i-it or a letter"

    3:   isalnu.( '8' # @ "8 is a " 4 "8 is not a "

    3   "i-it or a letter" #;

    3<   rintf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n"

    32   ">ccorin- to isi-it4"

    3?   isi-it( '' # @ " is a " 4 " is not a "

    70   "heaeci.al i-it"71   isi-it( 'A' # @ "A is a " 4 "A is not a "

    73   "heaeci.al i it"

  • 8/19/2019 C_chap08.ppt

    10/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    Program Output

    77   isi-it( '

  • 8/19/2019 C_chap08.ppt

    11/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.4 String Conversion Functions

    • Conversion "unctions #  In stlib+h, general utilities lirar!

     #  Convert strings o" digits to integer and "loating-point values

     

    Protot!pe /escription

    "ouble atof( const char *nPtr # 

    Converts the string nPtr to "ouble.int atoi( const char *nPtr #  Converts the string nPtr to int.

    lon- atol( const char *nPtr #  Converts the string nPtr to long int.

    "ouble strto"( const char *nPtr char**en"Ptr # 

    Converts the string nPtr to "ouble+ 

    lon- strtol( const char *nPtr char

    **en"Ptr int base # 

    Converts the string nPtr to lon-. 

    unsi-ne" lon- strtoul( const char *nPtrchar **en"Ptr int base # 

    Converts the string nPtr to unsi-ne"lon-. 

  • 8/19/2019 C_chap08.ppt

    12/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variable

    2. Convert string

    2.1 Assign to variable

    3. Print

    Program Output

    1 /* i-+ 2+4 fi-0250+c

    3   6sin- atof */

    7 8inclue stio+h, 

    9 8inclue stlib+h, 

    :

    int .ain(#

    <

    2   ouble ;

    ?

    10   = atof( "??+0" #;

    11   rintf( "%s%+7f\n%s%+7f\n"

    13   "Che strin- \"??+0\" conerte to ouble is "

    17   "Che conerte alue iie b) 3 is "19   / 3+0 #;

    1:   return 0;

    1 B

    Che strin- "??+0" conerte to ouble is ??+000

    Che conerte alue iie b) 3 is 9?+:00

  • 8/19/2019 C_chap08.ppt

    13/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.5 Standard Input/Output LibraryFunctions

    • unctions in stio+h,  #  sed to manipulate character and string data

    Function prototype Function description

    int -etchar( 0oi" #;  Inputs the next character from the

    standard input and returns it as an integer.

    char *-ets( char *s #; 

    Inputs characters from the standard inputinto the array s until a newline or end-of-

    file character is encountered. Aterminating null character is appended tothe array.

    int utchar( int c #;  Prints the character stored in c.

    int uts( const char *s #;  Prints the string s followed by a newline

    character.

    int srintf( char *s const char*for.at +++ #;

     

    $(uivalent to rintf, e%cept the outputis stored in the arra! s instead o"

     printing it on the screen.

    int sscanf( char *s const char

    *for.at +++ #; 

    $(uivalent to scanf, e%cept the input is

    read "rom the arra! s instead o"

    reading it "rom the &e!oard.

    /

  • 8/19/2019 C_chap08.ppt

    14/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Input

    3. Print

    3.1 Function definition

    (note recursion)

    1 /* i-+ 2+174 fi-02517+c

    3   6sin- -ets an utchar */

    7 8inclue stio+h, 

    9

    : int .ain(#

    <   char sentence[ 20 ];

    2   oi  reerse( const char * const #;?  

    10   rintf( "nter a line of tet4\n" #;

    11   -ets( sentence #;

    13

    17   rintf( "\nChe line rinte bacD!ars is4\n" #;

    19   reerse( sentence #;

    1:

    1   return 0;1< B

    12

    1? oi  reerse( const char * const sPtr #

    30

    31   if ( sPtr[ 0 ] == '\0' #

    33   return;

    37   else 

    39   reerse( sPtr[ 1 ] #;

    3:   utchar( sPtr[ 0 ] #;

    3   B

    3< B

    nter a line of tet4Eharacters an Ftrin-s

     Che line rinte bacD!ars is4s-nirtF na sretcarahE

    reerse calls itsel" using sustrings o" theoriginal string. )hen it reaches the'\0' 

    character it prints using utchar

  • 8/19/2019 C_chap08.ppt

    15/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.6 String Manipulation Functions of theString Handling Library

    • String handling lirar! has "unctions to #  9anipulate string data

     #  Search strings

     #  'o&eni7e strings

     #  /etermine string length

     

    Function prototype Function description

    char *strc)( char *s1const char *s3 #

     

    Copies string s3 into arra! s1. 'he value o" s1 isreturned.

    char *strnc)( char *s1const char *s3 size5t n #

     

    Copies at most n characters o" string s3 into arra!s1. 'he value o" s1 is returned.

    char *strcat( char *s1const char *s3 #

     

    Appends string s3 to arra! s1. 'he "irst character o"s3 over*rites the terminating null character o" s1.'he value o" s1 is returned.

    char *strncat( char *s1const char *s3 size5t n #

     

    Appends at most n characters o" string s3 to arra!s1. 'he "irst character o" s3 over*rites theterminating null character o" s1. 'he value o" s1 isreturned.

    1 /* i 2 1? fi 02 1?

  • 8/19/2019 C_chap08.ppt

    16/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Function calls

    3. Print

    Program Output

    1 /* i-+ 2+1?4 fi-0251?+c

    3   6sin- strcat an strncat */

    7 8inclue stio+h, 

    9 8inclue strin-+h, 

    :

    int .ain(#

    <

    2   char s1[ 30 ] = "Ha) ";

    ?   char s3[] = "Ge! ear ";

    10   char s7[ 90 ] = "";

    11  

    13   rintf( "s1 = %s\ns3 = %s\n" s1 s3 #;

    17   rintf( "strcat( s1 s3 # = %s\n" strcat( s1 s3 # #;19   rintf( "strncat( s7 s1 # = %s\n" strncat( s7 s1 # #;

    1:   rintf( "strcat( s7 s1 # = %s\n" strcat( s7 s1 # #;

    1   return 0;

    1< B

    s1 = Ha)s3 = Ge! earstrcat( s1 s3 # = Ha) Ge! earstrncat( s7 s1 # = Ha)strcat( s7 s1 # = Ha) Ha) Ge! ear

  • 8/19/2019 C_chap08.ppt

    17/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.7 Comparison Functions of the StringHandling Library

    • Comparing strings #  Computer compares numeric ASCII codes o" characters in string

     #  Appendi% / has a list o" character codes

    • int strc.( const char *s1 const char *s3 #;

     #  Compares string s1 to s3

     #  eturns a negative numer s1   s3, 7ero s1 == s3, or a positive numer s1 ,  s3

    • int strnc.( const char *s1 const char *s3size5t n #;

     #  Compares up to n characters o" string s1 to s3

     #  eturns values as aove

  • 8/19/2019 C_chap08.ppt

    18/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.8 Search Functions of the String HandlingLibrary

    Function prototype Function description

    char *strchr( const char *sint c #; 

    3ocates the "irst occurrence o" character c in string s. I" c is "ound, a pointer to c in

    s is returned. 5ther*ise, a G6II pointer is returned. 

    size5t strcsn( const char*s1 const char *s3 #; 

    /etermines and returns the length o" the initial segment o" string s1 consisting o"characters not contained in string s3. 

    size5t strsn( const char

    *s1 const char *s3 #; 

    /etermines and returns the length o" the initial segment o" string s1 consisting onl!

    o" characters contained in string s3. 

    char *strbrD( const char*s1 const char *s3 #; 

    3ocates the "irst occurrence in string s1 o" an! character in string s3. I" a character"rom string s3 is "ound, a pointer to the character in string s1 is returned. 5ther-*ise, a G6II pointer is returned. 

    char *strrchr( const char *sint c #; 

    3ocates the last occurrence o" c in string s. I" c is "ound, a pointer to c in string s isreturned. 5ther*ise, a G6II pointer is returned. 

    char *strstr( const char *s1

    const char *s3 #; 

    3ocates the "irst occurrence in string s1 o" string s3. I" the string is "ound, a pointer

    to the string in s1 is returned. 5ther*ise, a G6II pointer is returned. 

    char *strtoD( char *s1 constchar *s3 #; 

    A se(uence o" calls to strtoD rea&s string s1 into :to&ens;6logical pieces suchas *ords in a line o" te%t6separated ! characters contained in string s3. 'he "irstcall contains s1 as the "irst argument, and suse(uent calls to continue to&eni7ingthe same string contain G6II as the "irst argument. A pointer to the current to&en isreturned ! each call. I" there are no more to&ens *hen the "unction is called, G6II is returned. 

    1 /* i- 2 3

  • 8/19/2019 C_chap08.ppt

    19/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Function calls

    3. Print

    Program Output

    1 /* i-+ 2+3

  • 8/19/2019 C_chap08.ppt

    20/25© 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Function calls

    3. Print

    Program Output

    1 /* i-+ 2+3?4 fi-0253?+c

    3   6sin- strtoD */

    7 8inclue stio+h, 

    9 8inclue strin-+h, 

    :

    int .ain(#

    <

    2   char strin-[] = "Chis is a sentence !ith < toDens";

    ?   char *toDenPtr;

    10  

    11   rintf( "%s\n%s\n\n%s\n"

    13   "Che strin- to be toDenize is4" strin-

    17   "Che toDens are4" #;

    19  

    1:   toDenPtr = strtoD( strin- " " #;

    1

    1<    !hile ( toDenPtr J= G6II #

    12   rintf( "%s\n" toDenPtr #;

    1?   toDenPtr = strtoD( G6II " " #;

    30   B

    31

    33   return 0;

    37 B

    Che strin- to be toDenize is4Chis is a sentence !ith < toDens Che toDens are4Chisisasentence

    !ith<toDens

  • 8/19/2019 C_chap08.ppt

    21/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.9 Memory Functions of the String-handling Library

    • 9emor! unctions #  In stlib+h, 

     #  9anipulate, compare, and search loc&s o" memor!

     #  Can manipulate an! loc& o" data

    • Pointer parameters are oi * #  An! pointer can e assigned to oi *, and vice versa

    – oi * cannot e dere"erenced

    • $ach "unction receives a si7e argument speci"!ing the numero" !tes characters to process

  • 8/19/2019 C_chap08.ppt

    22/25© 2000 Prentice Hall, Inc. All rights reserved.

    8.9 Memory Functions of the String-

    handling Library (II) 

    Protot!pe /escription

    oi" *.e.c)( oi" *s1 const oi" *s3size5t n #

     

    Copies n characters "rom the o

  • 8/19/2019 C_chap08.ppt

    23/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Function calls

    3. Print

    Program Output

    1 / i-+ 2+734 fi-02573+c

    3   6sin- .e..oe */

    7 8inclue stio+h, 

    9 8inclue strin-+h, 

    :

    int .ain(#

    < 2   char [] = "Ho.e F!eet Ho.e";

    ?  

    10   rintf( "%s%s\n"

    11   "Che strin- in arra) before .e..oe is4 " #;

    13   rintf( "%s%s\n"

    17   "Che strin- in arra) after .e..oe is4 "

    19   .e..oe( [ : ] 10 # #;1:

    1   return 0;

    1< B

    Che strin- in arra) before .e..oe is4 Ho.e F!eet Ho.eChe strin- in arra) after .e..oe is4 F!eet Ho.e Ho.e

  • 8/19/2019 C_chap08.ppt

    24/25

    © 2000 Prentice Hall, Inc. All rights reserved.

    8.10Other Functions of the String HandlingLibrary

    • char *strerror( int errornu. #;  #  Creates a s!stem-dependent error message ased on errornu. 

     #  eturns a pointer to the string

    • size5t strlen( const char *s #;  #  eturns the numer o" characters e"ore G6II in string s

    1 /* i-+ 2+7

  • 8/19/2019 C_chap08.ppt

    25/25

    Outline

    1. Function call

    2. Print

    Program Output

    1 / i-+ 2+7