Strings - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Strings

Description:

'guitar' 'shoe' 'along' Things about Strings. that are so interesting ... User can type a string longer than the array you have set up. This will result in a crash! ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 13
Provided by: jeffch8
Category:
Tags: guitar | strings

less

Transcript and Presenter's Notes

Title: Strings


1
Strings
2
Famous Strings in History
  • cheese
  • g
  • guitar
  • shoe
  • along

3
Things about Stringsthat are so interesting
  • Made up of individual characters
  • Denoted by double quotes
  • Have invisible character at end called null
    terminator character (\0)
  • How many characters in the word cat?

Answer 4 - you forgot the null terminator!!
4
Declaring Strings
  • Can do multiple ways
  • char charArray 255 //set size of array to 255
  • char charArray blah blah blah
  • // sets charArray pointing to space thats 15 big
  • Both ways accomplish the same thing because both
    are pointers to memory (even though you dont
    realize it!)

5
Reading String from the User
  • Use cin to get a word
  • Example
  • char charArray 255
  • cin gtgt charArray
  • Use cin.getline ( ) to get entire line of text
  • Example
  • cin.getline (charArray, 255)

6
BSOD
  • There is a danger when getting input
  • User can type a string longer than the array you
    have set up
  • This will result in a crash! (trying to go into
    someone elses memory)
  • Use setw( ) to solve!
  • Example
  • cin gtgt setw (255) gtgt charArray

Ensure input does not exceed size of array
7
Printing Strings
  • Simple use cout to do this
  • Example
  • cout ltlt charArray ltlt endl

8
String Functions
  • Whole library dedicated to strings
  • include ltstring.hgt
  • strcpy (char , char ) // returns a char !!!!
  • strcat (char , char ) // returns a char !!!!
  • strcmp (char , char ) // returns an int
  • strtok (char , char ) // returns char

9
strcmp ( )
  • Used to compare two strings
  • CANNOT TYPE
  • if (string1 string2)
  • Example
  • int result strcmp (Hello, World)
  • // if result lt 0, then hello is lt world
  • // if result 0, then hello has the same
    chars
  • // if result gt 0, then hello gt world (but
    its not)

10
strtok ( )
  • Token - sequence of chars separated by some
    delimiter
  • Example
  • When,in,the,course,of,human,events,it, becomes
  • What are tokens?
  • What is delimiter?

Answer the words!
Answer the comma - so we say this is a comma
delimited string of tokens
11
Usage(Pass NULL for each successive call)
  • include ltiostream.hgt
  • include ltstring.hgt
  • void main ( )
  • char charArray Jeff Rox the World
  • char tokenPtr
  • tokenPtr strtok (charArray, )
  • while (tokenPtr !NULL)
  • cout ltlt tokenPtr ltlt endl
  • tokenPtr strtok (NULL, )

Notice the NULL!
Output Jeff Rox the World
12
More Code
  • void main ( )
  • char charArray Jeff Rox the World
  • char tokenPtr
  • tokenPtr strtok (charArray, e)
  • while (tokenPtr !NULL)
  • cout ltlt tokenPtr ltlt endl
  • tokenPtr strtok (NULL, e)

Output J ff Rox th World
Write a Comment
User Comments (0)
About PowerShow.com