In Addition . . . - PowerPoint PPT Presentation

About This Presentation
Title:

In Addition . . .

Description:

... character (which is not consumed, but remains waiting in the input stream) ... (syntax) of get function together with an input stream variable, such as cin is: ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 32
Provided by: dma77
Category:
Tags: addition | stream

less

Transcript and Presenter's Notes

Title: In Addition . . .


1
In Addition . . .
  • To the string class from the standard library
    accessed by include ltstringgt
  • C also has another library of string functions
    for C strings that can be accessed by include
    ltcstringgt

1
2
What is a C String?
  • A C string is a char array terminated by the null
    character \0 ( with ASCII value 0 ).
  • A C string variable can be initialized in its
    declaration in two equivalent ways.
  • char message 8 H, e, l, l,
    o, \0
  • char message 8 Hello

3
char vs. C string
  • A has data type char
  • and is stored in 1 byte
  • A is a C string of 2 characters
  • and is stored in 2 bytes

4
Recall that . . .
  • char message8 // declaration allocates
    memory
  • To the compiler, the value of the identifier
    message alone is the base address of the array.
    We say message is a pointer (because its value is
    an address). It points to a memory location.

5
Aggregate C String I/O in C
  • I/O of an entire C string is possible using the
    array identifier with no subscripts and no
    looping.

EXAMPLE char message 8 cin gtgt
message cout ltlt message HOWEVER
. . .
6
Extraction operator gtgt
  • When using the extraction operator ( gtgt ) to
    read input characters into a string variable,
  • the gtgt operator skips any leading whitespace
    characters such as blanks and newlines
  • it then reads successive characters into the
    array, and stops at the first trailing whitespace
    character (which is not consumed, but remains
    waiting in the input stream)
  • the gtgt operator adds the null character to the
    end of the string

7
Example Using gtgt
  • char name 5
  • cin gtgt name
  • Suppose input stream looks like this
  • J o e

total number of elements in the array
7000
J o e \0
name 0 name 1 name 2 name 3
name 4
null character is added
8
Function get( )
  • because the extraction operator stops reading at
    the first trailing whitespace, gtgt cannot be used
    to input a string with blanks in it
  • if your strings declared size is not large
    enough to hold the input characters and add the
    \0, the extraction operator stores characters
    into memory beyond the end of the array
  • use get function with 2 parameters to overcome
    these obstacles
  • EXAMPLE
  • char message 8
  • cin.get ( message, 8 ) // inputs at most 7
    characters plus \0

9
inFileStream.get ( str, count 1)
  • get does not skip leading whitespace characters
    such as blanks and newlines
  • get reads successive characters (including
    blanks) into the array, and stops when it either
    has read count characters, or it reaches the
    newline character \n, whichever comes first
  • get appends the null character to str
  • if it is reached, newline is not consumed by get,
    but remains waiting in the input stream

10
Function ignore( )
  • can be used to consume any remaining characters
    up to and including the newline \n left in the
    input stream by get
  • EXAMPLE
  • cin.get ( string1, 81 ) // inputs at most
    80 characters
  • cin.ignore ( 30, \n ) // skips at most
    30 characters // but stops if \n
    is read
  • cin.get ( string2, 81 )

11
Another Example Using get( )
  • char ch
  • char fullName 31
  • char address 31
  • cout ltlt Enter your full name
  • cin.get ( fullName, 31 )
  • cin.get (ch) // to
    consume the newline
  • cout ltlt Enter your address
  • cin.get ( address, 31 )

N e l l D a
l e \0 . . .
fullName 0
A u s t i n
T X \0 . . .
address 0
12
String Function Prototypes inltcstring gt
  • int strlen (char str )
  • // FCTNVAL integer length of string str (
    not including \0 )
  • int strcmp ( char str1 , char str2 )
  • // FCTNVAL negative, if str1 precedes str2
    lexicographically
  • // positive, if str1 follows str2
    lexicographically
  • // 0, if str1 and str2 characters same
    through \0
  • char strcpy ( char toStr , char fromStr
    )
  • // FCTNVAL base address of toStr ( usually
    ignored )
  • // POSTCONDITION characters in string fromStr
    are copied to
  • // string toStr, up to and including \0,
  • // overwriting contents of string toStr

13
  • include ltcstring gt
  • .
  • .
  • .
  • char author 21
  • int length
  • cin.get ( author , 21 )
  • length strlen ( author ) // What is the
    value of length ?

14
  • char myName 21 Huang // WHAT IS
    OUTPUT?
  • char yourName 21
  • cout ltlt Enter your last name
  • cin.get ( yourName, 21 )
  • if ( strcmp ( myName, yourName ) 0 )
  • cout ltlt We have the same name!
  • else if ( strcmp ( myName, yourName ) lt 0 )
  • cout ltlt myName ltlt comes before ltlt
    yourName
  • else if ( strcmp ( myName, yourName ) gt 0 )
  • cout ltlt yourName ltlt comes before ltlt
    myName

H u a n g \0
. . .
myName 0
H e a d i n g t
o n \0 . . .
yourName 0
15
  • char myName 21 Huang
  • char yourName 21
  • if ( myName yourName ) // compares
    addresses only!
  • // That is, 4000 and 6000 here.
  • . // DOES NOT COMPARE CONTENTS!
  • .
  • .

4000
H u a n g \0
. . .
myName 0
6000
H e a d i n g t
o n \0 . . .
yourName 0
16
  • char myName 21 Huang
  • char yourName 21
  • cin.get ( yourName, 21 )
  • yourName myName // DOES NOT COMPILE!
  • // What is the value of myName ?

4000
H u a n g \0
. . .
myName 0
6000
H e a d i n g t
o n \0 . . .
yourName 0
17
  • char myName 21 Huang
  • char yourName 21
  • cin.get ( yourName, 21 )
  • strcpy ( yourName, myName ) // changes
    string yourName
  • // OVERWRITES CONTENTS!

4000
H u a n g \0
. . .
myName 0
6000
u n g \0
H e a d i n g t
o n \0 . . .
yourName 0
18
  • There is a difference between 'A' and "A". The
    first one is character A and the second is string
    A.
  • Since strings are null terminated, "A" represents
    two characters, 'A' and '\0'.
  • "Hello" represents six characters, 'H', 'e', 'l',
    'l', 'o', and '\0'.
  • To store 'A' we need only one memory cell of the
    type char, while to store "A", we need two memory
    cells of the type char, one for 'A' and the other
    for '\0'.
  • To store the string "HELLO" in computer we need
    six memory cells of the type char.
  • Consider the statement.
  • char name16
  • Since C strings are null terminated and name has
    sixteen components, the largest string that can
    be stored in name is 15.
  • If you store a string of length, say 10 in name,
    the first 11 components of name are used and the
    last 5 are left unused.

19
  • char name16 'J', 'o', 'h', 'n', '\0'
  • During char array variable declaration, C
    allows the string notation to be used in the
    initialization statement.
  • char name16 "John" //Line A
  • The statement
  • char name "John" //Line B
  • declares a string variable name of length large
    enough, that is, 5 (here) and stores "John" in
    it.
  • There is a difference between the last two
    statements. Both statements stores "John" in
    name.
  • The size of name in the statement in Line A is 16
    while its size in the statement in Line B is 5.

20
  • Most of the rules that apply to other arrays also
    apply to character arrays.
  • Consider the statement
  • char studentName26
  • studentName "Lisa L. Johnson" //Illegal

21
  • String Comparison
  • In C, C-strings are compared character by
    character using the collating sequence of the
    system.
  • Let us assume that we are using the ASCII
    character set.
  • 1. The string "Air" is smaller than the string
    "Boat".
  • 2. The string "Air" is smaller than the string
    "An".
  • 3. The string "Bill" is smaller than the string
    "Billy".
  • 4. The string "Hello" is smaller than "hello".

22
  • Example 9-7
  • char studentName21
  • char myname16
  • char yourname16
  • Statement Effect
  • a. strcpy(myname,"John Robinson") mynameJohn
    Robinson
  • b. strlen("John Robinson") Returns 13, the
    length
  • of the string
  • c. int len
  • len strlen ("Sunny Day") Stores 9 into len
  • d. strcpy(yourname,"Lisa Miller") yourname
    Lisa Miller
  • strcpy(studentName,yourname) studentName
    Lisa Miller
  • e. strcmp("Bill", "Lisa") Returns a value lt
    0
  • f. strcpy(yourname, "Kathy Brown") yourname
    Kathy Brown
  • strcpy(myname, "Mark G. Clark") myname
    Mark G. Clark
  • strcmp(myname,yourname) Return a value gt 0

23
  • Reading and Writing Strings
  • char name31
  • String Input
  • Since aggregate operations are allowed for string
    input, the statement
  • cingtgtname
  • will store the next input string into name.
  • Strings that contain blanks cannot be read using
    the extraction operator gtgt.

24
  • To read strings with blanks, the general form
    (syntax) of get function together with an input
    stream variable, such as cin is
  • cin.get(str,m1)
  • This statement stores the next m characters or
    until the newline character '\n' is found into
    str. The newline character is not stored in str.
  • If the input string has fewer than m characters,
    then the reading stops at the newline character.

25
  • Consider the following statements
  • char str31
  • cin.get(str,31)
  • If the input is
  • William T. Johnson
  • then "William T. Johnson" will be stored in str
    and
  • If the input is
  • Hello there. My name is Mickey Mouse.
  • then the string
  • "Hello there. My name is Mickey"

26
  • char str126
  • char str226
  • char discard
  • Input is, say
  • Summer is warm.
  • Winter will be cold.
  • The following statements store line 1 in str1 and
    line 2 in str2.
  • cin.get(str1,26)
  • cin.get(discard)
  • cin.get(str2,26)

27
  • String Output
  • The statement
  • coutltltname
  • will output the content of name on the screen.
  • The insertion operator ltlt continues to write the
    contents of name until it finds the null
    character.
  • If name does not contain the null character, then
    we will see strange output since ltlt continues to
    output data from memories adjacent to name until
    '\0' is found.

28
  • Specifying Input/Output Files at the Execution
    Time
  • ifstream infile
  • ofstream outfile
  • char fileName51
  • coutltlt"Enter the input file name "
  • cingtgtfileName
  • infile.open(fileName) //open the input file
  • .
  • .
  • .
  • coutltlt"Enter the output file name "
  • cingtgtfileName
  • outfile.open(fileName) /open the output file

29
  • The string type and Input/Output Files
  • Values (that is, strings) of the type string are
    not null terminated.
  • Variables of type string can also be used to read
    and store the names of input/output files.
  • The argument to the function open must be a null
    terminated string, that is a C string.
  • if we use a variable of the type string to read
    the name of an input/output file and then use
    this variable to open a file, the value of the
    variable must (first) be converted to a C-string
    (that is, a null-terminated string).

30
  • The header file string contains the function
    c_str, which converts a value of the type string
    to a null-terminated character array (that is,
    C-string).
  • The syntax to use the function c_str is
  • strVar.c_str()
  • where strVar is a variable of the type string.

31
  • ifstream infile
  • string fileName
  • coutltlt"Enter the input file name "
  • cingtgtfileName
  • infile.open(fileName.c_str()) //open the input
    file
Write a Comment
User Comments (0)
About PowerShow.com