C Style Strings Chapter 5 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

C Style Strings Chapter 5

Description:

Character strings are simply arrays of characters ... Leave room for the null character in the array declaration ... Dangling newlines ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 15
Provided by: cseMs
Category:

less

Transcript and Presenter's Notes

Title: C Style Strings Chapter 5


1
C Style StringsChapter 5
2
C-style Character Strings in C/C
  • Character strings are simply arrays of characters
  • All character strings are assumed to be null
    terminated (end in \0 character)
  • Leave room for the null character in the array
    declaration
  • Literal character strings are in " "
    (individual characters are in ' ')

3
Examples
  • char firstName "Stephen"
  • char lastName "Smith"
  • These are strings of 8 and 6 characters
    respectively.
  • Same as
  • char firstName S, t, e, p, h,
    e, n, \0
  • char lastName S, m, I, t, h,
    \0

4
Output of character strings
  • Output of literal or string variables to output
    streams will output all characters up to the null
    character.
  • char bookTitle Rainmaker
  • char authorName John Grisham
  • cout ltlt The book is ltlt bookTitle
  • ltlt by ltlt authorName ltlt endl

5
Input of character strings
  • Input of character strings is similar to input of
    other types - input values separated by white
    spaces
  • char bookTitle10
  • char authorName13
  • cin gtgt bookTitle gtgt authorName
  • if input is
  • Rainmaker John Grisham
  • then
  • bookTitle is Rainmaker and authorName
    is John

6
Input of whole lines of characters
  • The extraction operator (gtgt) is not safe because
    if the input string has more characters than the
    variable, it reads anyway.
  • The getline member function of the istream class
    can be used to read an entire line (including
    whitespace) into a string variable
  • char line80
  • cin.getline (line, 80 ,\n)

7
Dangling newlines
  • When numeric values are input, the following
    whitespace character is not processed

if input is 30 The next line then
string \n
int number char string80 cin gtgt
number cin.getline(string,80,\n)
Correct version string The next line
cingtgtnumber cin.ignore(80,\n) cin.getline(stri
ng,80,\n)
8
C-style String functions
  • Set of built-in functions for assignment,
    comparison, and other manipulation of character
    strings.
  • You have to use
  • include ltcstringgt

9
Copying
  • Cant use the assignment operator () to assign
    the contents of one character string to another.
  • You can use the strcpy function instead.
  • The format is
  • strcpy(destString , srcString )
  • Example
  • strcpy(myName, yourName)

10
Finding the Length
  • The strlen function returns the length of the
    string.
  • It doesnt count the null character
  • Example
  • strlen(Mahmood Hossain) returns 15

11
Comparing Two Strings
  • Cant use the comparison/equality operators to
    compare two character strings.
  • You can use strcmp function.
  • The format is
  • int strcmp (char string1 , char string2
    )
  • Returns
  • 0 if string1 lexically string2
  • -ve if string1 lexically lt string2
  • ve if string1 lexically gt string2
  • Example
  • strcmp(Robinson, Roberts)

12
Example
  • include ltcstring.hgt
  • void main (void)
  • char myName Mahmood
  • char anotherName30
  • char otherName Jason
  • strcpy (anotherName, myName)
  • cout ltlt anotherName ltlt endl
  • cout ltlt Length of my name is ltlt
    strlen(myName) ltlt endl
  • if (strcmp (myName, otherName) 0)
  • cout ltlt Names are the same ltlt endl
  • else if (strcmp (myName, otherName) lt 0)
  • cout ltlt myName ltlt precedes ltlt
    otherName ltlt endl
  • else
  • cout ltlt otherName ltlt precedes ltlt
    myName ltlt endl

13
Arrays of Strings
  • A string is a 1-D array of characters, so a 2-D
    array of characters would be an array of strings
  • char names410 "Robert","Adam",

  • "Charles","Benjamin"
  • This will declare an array of 4 strings with each
    string having a maximum of 10 characters
  • names0 names1 names2 names3

14
Converting strings to numbers
  • Functions defined in ltcstdlibgt
  • String to double
  • double atof(char string)
  • String to int
  • int atoi(char string)
  • String to long
  • long atol(char string)
  • Conversion begins at the first character and
    skips leading whitespaces
  • Conversion ends at the first inappropriate
    character or the end of the string (null)
Write a Comment
User Comments (0)
About PowerShow.com