string what it is why it’s useful library routines for handling strings how to input a string from...
Post on 22-Dec-2015
222 views
TRANSCRIPT
String
• What it is
• Why it’s useful
• library routines for handling strings
• how to input a string from the keyboard
strings
What’s the difference between these two declarations?
char letter [ ] = {‘A’};
char letter[ ] = { “A”};
strings
What’s the difference between these two declarations?
char letter [ ] = {‘A’};
char letter[ ] = { “A”};
letter A
letter ANULL
array of characterschar digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’};
‘0’‘1’
‘3’‘4’‘5’‘6’‘7’‘8’‘9’
digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
‘2’How many elements does thedigits array contain?
string variablechar digits [ ] = {“0123456789”};
‘0’‘1’
‘3’‘4’‘5’‘6’‘7’‘8’‘9’
digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
‘2’How many elements does thedigits string contain?
NULL
string variablechar digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’\0’};
‘0’‘1’
‘3’‘4’‘5’‘6’‘7’‘8’‘9’
digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
‘2’
‘\0’
Why are strings useful?
• Can treat a string as a unit instead of dealing with it as a bunch of individual characters.
Outputting a message without strings
Suppose you wanted to output the message “I love CS1005!” using an array of characters.
const int size = 14; char message [size] = {'I', ' ', 'l', 'o', 'v', 'e', ' ','C', 'S', '1', '0', '0', '5', '!'};
for (int i = 0; i < size; i++) { cout << message[i]; }
cout << endl;
outputting a message using a string
char Message [] = {“I Love CS1005!” };cout << Message << endl;
Note that only the name of the array is provided to cout not the size.
cout Starts at the first element of the Message array.cout Stops when NULL character is encountered.
outputting a message using a string
‘ ’
‘o’‘v’‘e’‘ ’‘C’‘S’‘1’
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
‘l’
NULL
‘I’
‘0’‘0’‘5’‘!’
cout << “I Love CS1005!”;
Compiler creates char array in memory terminated by NULL character.
This is called a string constant. It is an unnamed array and cannot be changed.
Now you know how it’s done!
Exercises
1) Write a code segment that displays your name.Create a string variable (not a string constant). Use cout to display it
2) What is the size of the string variable containing your name?
string handling library functions
• www.cplusplus.com
• strcpy - copies one string to another
• strcat - appends one string to another
• strlen - returns the length of a string
copying arrays
const int size = 6; char String1[ ] = {“Hello”}; char Sentence[100];
Sentence = String1; // ERROR!
would have to copy it one character at a time. for (int i = 0; i < size; i++) { Sentence [i] = String1[i]; }
using strcpy
strcpy (Destination, Source);
char String1[ ] = {“Hello”}; char Sentence[100];
strcpy (Sentence, String1);
cout << Sentence << endl;
displays Hello
‘H’‘e’
‘l’‘o’
NULL
Sentence[0] [1] [2] [3] [4] [5]
‘l’
using strcat strcat (Destination, Source);
char String1[ ] = {“Hello”}; char String2[ ] = {“ World!};char Sentence[100];
strcpy (Sentence, String1); strcat (Sentence, String2); cout << Sentence << endl;
displays Hello World!
Note: strcpy copied a NULL char at the end of String1. strcat replaced that NULL with beginning of String2
‘H’‘e’
‘l’‘o’
Sentence[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
‘l’
‘W’‘o’‘r’‘l’‘d’‘!’
NULL’
using strlen
char String1[ ] = {“Hello”}; int length = strlen (String1);
cout << length << endl;
Displays 5
Note: strlen returns the length of the string excluding the NULL character.
Creating sentences example
Write a program that uses a random number generator to create sentences.
The program should use 4 arrays of strings called article, noun, verb and preposition.
The program should create a sentence by selecting a word at random from each array in the following order: article, noun, verb, proposition, article, noun.
As each word is picked it should be concatenated to the previous words in the sentence.
Array of strings
const int Size = 5; const int WordSize = 10;
char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char noun [Size] [WordSize] = {"boy", "girl", "dog", "town", "car"}; char verb [Size] [WordSize] = {"drove", "jumped", "ran", "walked", "skipped"}; char preposition [Size] [WordSize] = {"to", "from", "over", "under", "on"};
Choose word from array randomly
const int Size = 5;const int WordSize = 10;char article [Size] [WordSize] = {"the", "a", "one", "some", "any"};
int index; srand((unsigned) time (NULL));
index = rand() % Size;
Copy word to sentence
const int Size = 5;const int WordSize = 10;char article [Size] [WordSize] = {"the", "a", "one", "some", "any"};char Sentence[6 * WordSize];
int index; srand((unsigned) time (NULL));
index = rand() % Size; strcpy (Sentence, article[index]);
Concatenate space after word
const int Size = 5;const int WordSize = 10;char article [Size] [WordSize] = {"the", "a", "one", "some", "any"};char Sentence[6 * WordSize];
int index; srand((unsigned) time (NULL));
index = rand() % Size; strcpy (Sentence, article[index]);strcat (Sentence, “ “);
Create sentence code const int Size = 5;
const int WordSize = 10;
char article [Size] [WordSize] = {"the", "a", "one", "some", "any"};
char noun [Size] [WordSize] = {"boy", "girl", "dog", "town", "car"};
char verb [Size] [WordSize] = {"drove", "jumped", "ran", "walked",
"skipped"};
char preposition [Size] [WordSize] = {"to", "from", "over", "under",
"on"};
char Sentence[6 * WordSize];
int index;
srand((unsigned) time (NULL));
Create sentence code (cont) for (int i = 0; i < 10; i++) {
index = rand() % Size;
strcpy (Sentence, article[index]);
strcat (Sentence, " ");
index = rand() % Size;
strcat (Sentence, noun[index]);
strcat (Sentence, " ");
index = rand() % Size;
strcat (Sentence, verb[index]);
strcat (Sentence, " ");
Create sentence code (cont) index = rand() % Size;
strcat (Sentence, preposition[index]);
strcat (Sentence, " ");
index = rand() % Size;
strcat (Sentence, article[index]);
strcat (Sentence, " ");
index = rand() % Size;
strcat (Sentence, noun[index]);
cout << Sentence << endl << endl;
}
Exercise
char Sentence [6 * WordSize] contains a sentence to display.Capitalize the first letter of the sentence using the library function toupper.
int toupper( int c );
inputting a string using cin
const maxSize = 20; char inputMessage[maxSize];
cin >> inputMessage;
cin reads in characters from the keyboard and puts them in the inputMessage array
cin doesn’t stop until it encounters whitespace.
Could read more characters than will fit in the array. !!
inputting a string using cin
const maxSize = 20; char inputMessage[maxSize];
cin >> setw(maxSize) >> inputMessage;
setw(maxSize) tells cin the size of the array is 20. cin will read in only 19 characters to leave room for the terminating NULL character.
cin does not store the NULL character for you.inputMessage[maxSize -1] = NULL;
inputting a string using cin
const maxSize = 20; char inputMessage[maxSize];
cin >> setw(maxSize) >> inputMessage; inputMessage[maxSize -1] = NULL;
inputting a string containing whitespace using cin.get
const int Size = 20;char Message [Size];cin.get (Message, Size);
cin.get continues reading characters into the Message array until: - Size -1 characters have been read - Newline character is encountered.
cin.get stores the NULL character immediately following the last character read.
Exercises
1) How many elements in vowels array: char vowels [] = {‘a’,’e’, ‘i’,’o’, ‘u’};
2) How many elements in vowels string: char vowels [] = {“aeiou”};
3) Find the error in the following code segment: const int Size = 7; char inputMessage[Size];
cin >> setw (Size) >> inputMessage;
// user types the word goodbye at the keyboard.