Download presentation
Presentation is loading. Please wait.
1
String What it is Why it’s useful
library routines for handling strings how to input a string from the keyboard
2
strings What’s the difference between these two declarations?
char letter [ ] = {‘A’}; char letter[ ] = { “A”};
3
strings What’s the difference between these two declarations?
char letter [ ] = {‘A’}; letter A char letter[ ] = { “A”}; letter A NULL
4
string array of characters Terminated with the NULL character. \0
5
array of characters char digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ‘0’ ‘1’ How many elements does the digits array contain? ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’
6
string variable char digits [ ] = {“0123456789”}; digits[0] [1] [2]
[3] [4] [5] [6] [7] [8] [9] [10] ‘0’ ‘1’ How many elements does the digits string contain? ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ NULL
7
string variable char digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’\0’}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ‘0’ ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ ‘\0’
8
Why are strings useful? Can treat a string as a unit instead of dealing with it as a bunch of individual characters.
9
Outputting a message without strings
Suppose you wanted to output the message “I love JEDDAH!” using an array of characters. const int size = 14; char message [size] = {‘I', ' ', 'l', 'o', 'v', 'e', ' ',‘J', ‘E', ‘D', ‘D', ‘A', ‘H', '!'}; for (int i = 0; i < size; i++) { cout << message[i]; } cout << endl;
10
outputting a message using a string
char Message [] = {“I Love JEDDAH!” }; 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.
11
outputting a message using a string
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] ‘I’ cout << “I Love JEDDAH!”; 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! ‘ ’ ‘l’ ‘o’ ‘v’ ‘e’ ‘ ’ ‘J’ ‘E’ ‘D’ ‘D’ ‘A’ ‘H’ ‘!’ NULL
12
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 variable???
13
string handling library functions
strcpy - copies one string to another strcat - appends one string to another strlen - returns the length of a string Cat = concatenated
14
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]; } Without strcpy would have to copy it one character at a time
15
using strcpy strcpy (Destination, Source);
char String1[ ] = {“Hello”}; char Sentence[100]; strcpy (Sentence, String1); cout << Sentence << endl; displays Hello Sentence[0] [1] [2] [3] [4] [5] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ NULL
16
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 Sentence[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’ NULL’
17
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.
18
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.
19
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"};
20
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. !! Problem with cin and more character in array which cause an error and whitespace -Error for having long string than the array can hold - Whitespace will stop entering string in array - Cin will store value in array by index inputMessage[i] by using for loop
21
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; - Setw will solve the problem of string overrun array size - Off course cin will not store null character for you. Null will be stored by default. - maxSize – 1 -> 20 – 1 = 19 -> null stored in this location - inputMessage[maxSize -1] = NULL will only show the location of null it is not a statement at all.
22
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. - Solving whitespace problem with cin.get - cin.get will include whitespace as an input for the array. - newline character is '\n'; it causes the insertion point to move to the beginning of the next line.
23
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. 1- 5 2- 6 3- e has no location in the array (the last position will be used for null character)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.