Strings - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Strings

Description:

... first name: Homer Simpson. Enter your last name: Your first name is Homer. Your last name is Simpson ... Marge 155480. Homer 865434. Krusty 454890. Ned 482241 ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 19
Provided by: adria9
Category:
Tags: marge | simpson | strings

less

Transcript and Presenter's Notes

Title: Strings


1
Strings
2
Strings
  • We have already been using constant character
    strings to display messages to the user.
  • Example
  • cout ltlt Hello world!
  • The constant character string is Hello world. In
    C, strings are simply an array with elements
    that are data type char.

3
Example
  • void main(void)
  • char firstName10
  • cout ltlt Enter your first name
  • cin gtgt firstName
  • cout ltlt Your name is ltlt firstName ltlt endl

Enter your first name Adrian Your name is Adrian
4
Example
  • In this example, we declared a string (or
    character array) of size 10. We can depict this
    array as

When the array is first created, its contents are
unknown. After the user enters the name Adrian
the content of the array becomes
Element that has index 6 is filled with the ASCII
0 or NULL character, which is also indicated as a
\0. This is the string termination character.
For example, when this string is sent to cout,
cout knows to stop displaying at element 5 and
does not display elements 7 to 9. Note that when
the user entered the string Adrian, this string
terminator was added automatically.
5
String Initialization
  • Strings can be initialized when they are declared
    like any other array type. The following
    statements are equivalent.
  • char b10 H, e, l, l, o, \0
  • char b10 Hello

Must explicitly include string terminator.
String terminator automatically added.
6
String Initialization
This following program produces a unexpected
output. Whats wrong with the code? The array
size is not large enough to include the string
terminator. When displaying firstName, cout will
continue to display the memory contents that
follow firstName until it happens to reach a \0
character.
  • / header /
  • include ltiostream.hgt
  • void main(void)
  • char firstName6 "Adrian"
  • cout ltlt "Your name is " ltlt firstName ltlt "!"

Should have an array size of 7 or greater
Your name is AdrianÏ(Ý!
7
User Input
In string initialization we can ensure that the
array is sufficiently large to hold the string we
want however, with user inputs, we do not have
any way of knowing how long of a string input
they will provide.
  • include ltiostream.hgt
  • void main(void)
  • char firstName10
  • cout ltlt Enter your first name
  • cin gtgt firstName
  • cout ltlt Your name is ltlt firstName ltlt endl

User input is larger than the array size. This
will cause a General Protection Fault because it
will overflow the array.
Enter your first name HomerSimpson
8
User Input
To avoid this overflow problem we can use setw()
to limit the number of characters to be copied
into the string.
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main(void)
  • char firstName10
  • cout ltlt Enter your first name
  • cin gtgt setw(10) gtgt firstName
  • cout ltlt Your name is ltlt firstName ltlt endl

Copies at most 9 characters (leaving an element
free for the string terminator)
Enter your first name HomerSimpson Your name is
HomerSimp
9
User Input
Although setw() limits the number of characters
that it copies, the remaining characters are in
the buffer and will be read during the next cin.
  • void main(void)
  • char firstName10
  • char lastName10
  • cout ltlt "Enter your first name "
  • cin gtgt setw(10) gtgt firstName
  • cout ltlt "Enter your last name "
  • cin gtgt setw(10) gtgt lastName
  • cout ltlt "Your first name is " ltlt firstName ltlt
    endl
  • cout ltlt "Your last name is " ltlt lastName ltlt
    endl

Enter your first name HomerSimpson Enter your
last name Your first name is HomerSimp Your last
name is son
10
User Input
Note that cin ignores leading whitespace (spaces,
tabs, newlines) and terminates the string when it
reaches any whitespace.following the string
  • void main(void)
  • char firstName10
  • char lastName10
  • cout ltlt "Enter your first name "
  • cin gtgt setw(10) gtgt firstName
  • cout ltlt "Enter your last name "
  • cin gtgt setw(10) gtgt lastName
  • cout ltlt "Your first name is " ltlt firstName ltlt
    endl
  • cout ltlt "Your last name is " ltlt lastName ltlt
    endl

Enter your first name Homer
Simpson Enter your last name Your first name is
Homer Your last name is Simpson
11
User Input
When can use cin.ignore(1000,\n) to discard the
remaining input.
  • void main(void)
  • char firstName10
  • char lastName10
  • cout ltlt "Enter your first name "
  • cin gtgt setw(10) gtgt firstName
  • cin.ignore(1000,\n)
  • cout ltlt "Enter your last name "
  • cin gtgt setw(10) gtgt lastName
  • cin.ignore(1000,\n)
  • cout ltlt "Your first name is " ltlt firstName ltlt
    endl
  • cout ltlt "Your last name is " ltlt lastName ltlt
    endl

Enter your first name Homer
Simpson Enter your last name Simpson Your first
name is Homer Your last name is Simpson
12
User Input
  • To read in phrases containing spaces use
    cin.getline(ltstringgt,ltlengthgt), which will read
    in characters until a newline character \n is
    encountered or ltlengthgt characters have been read
    in. These array of characters are copied into the
    ltstringgt variable. Note that the newline
    character is not copied into the string but the
    string terminator is automatically added.
  • Example
  • char str81
  • cin.getline(str,81)
  • In this example, the first 80 characters are
    copied and the remaining characters are left in
    the input buffer. The remaining characters can be
    discarded using cin.ignore(1000,\n)

13
Copying Strings
  • char firstName8 Adrian
  • char lecturerName8
  • lecturerName firstName
  • Like all arrays, strings cannot be copied
    directly. There is a library function
    strcpy(ltdestgt,ltsourcegt), which will copy the
    string (up to and including the string
    terminator) in the ltsourcegt to the ltdestgt. You
    must used the directive include ltstring.hgt
  • We can also copy constant strings into a variable
    as well.
  • strcpy(firstName,Adrian)
  • Note that strcpy does not check whether or not
    the size ltdestgt is sufficient to copy ltsourcegt.
    If it is too small, an array overflow will occur.

Will not work
14
Copying Strings
  • There is a library function strncpy(ltdestgt,ltsource
    gt,ltlengthgt), which operates the same as
    strcpy(ltdestgt,ltsourcegt), except that it includes
    a ltlengthgt variable.
  • strncpy will copy the string (up to and including
    the string terminator) in the ltsourcegt to the
    ltdestgt or it will stop copying if ltlengthgt
    characters are already copied.
  • char firstName8 Adrian
  • char lecturerName4
  • strncpy(lecturerName,firstName,4)
  • Note that in the example shown above, the
    variable lecturerName will contain Adri. This
    implies that there is no string terminator and it
    must be manually added using the code
  • lecturerName3 \0
  • The variable lecturerName will contain Adr\0 and
    now as the proper string terminator.

15
Length of String
The function strlen(ltstringgt) will compute the
number of characters in the string before the
string terminator.
  • char firstName8 Adrian
  • char lecturerName4
  • strncpy(lecturerName,firstName,4)
  • lecturerName3 \0
  • cout ltlt strlen(firstName) ltlt ltlt
    strlen(lecturerName) ltlt endl

6 3
16
Comparing Strings
  • char firstName8 Adrian
  • char lecturerName8 Adrian
  • if (lecturerName firstName)
  • cout ltlt This information is correct" ltlt endl
  • Like all arrays, strings cannot be compared
    directly. There is a library function
    strcmp(ltstr1gt,ltstr2gt) that can be used to compare
    two strings.
  • if (strcmp(firstName,lecturerName) 0)
  • cout ltlt This information is correct" ltlt endl

Will not work
17
Comparing Strings
  • strcmp(ltstr1gt,ltstr2gt) will return
  • 0 if ltstr1gt and ltstr2gt are identical
  • Something gt 0 if ltstr1gt comes after ltstr2gt in an
    alphabetical sense (e.g. dog comes after cat)
  • Something lt 0 if ltstr1gt comes before ltstr2gt in an
    alphabetical sense (e.g. cat comes before
    dog)
  • Two strings that are identical, but one is
    shorter than the other, the shorter comes before
    the other alphabetically.
  • Note strcmp is case sensitive. We can use
    stricmp(ltstr1gt,ltstr2gt) or strcasecmp(ltstr1gt,ltstr2gt
    ) for case-insensitive comparisons.

18
Problem 1 Student ID
Write a program that accepts a user input for a
name. The program reads in a data file
student.txt comparing one entry at a time to find
the entered name. The file student.txt is
formatted into two columns of data student name
and student ID number. The program then outputs
the matching student ID or an Entry not found
message if the name is not in the file. You may
assume that names are less than 20 characters in
length.
student.txt
  • Marge 155480
  • Homer 865434
  • Krusty 454890
  • Ned 482241

Enter a name Homer Homers ID is 865434
Enter a name Lisa Entry not found for Lisa
Write a Comment
User Comments (0)
About PowerShow.com