Engineering Problem Solving with C++, Etter Chapter 6 Strings 11-30-12 1
One Dimensional Arrays Character Strings The string Class. 2
C style strings functions defined in cstring CHARACTER STRINGS 3
C Style Character Strings A C style string is defined as a sequence of characters, terminated by the null character. When declaring a character array to store a C style string, memory must be allocated for the null character ('\0'). Literal string constants are enclosed within double quote marks: "a string". 4
C Style Character Strings Example: cout << "Hello"; 'H' 'e' 'l' 'l' 'o' '\0' 5
C Style String Input Recall that the input operator (>>) skips whitespace. To input strings with embedded whitespace, the getline() function can be used as illustrated: char phrase[size]; cin.getline(phrase, SIZE); The getline() function reads up to SIZE-1 characters from the input stream and will insert the null character. getline() is a member function of what class? stringin.cpp 6
C STYLE STRING FUNCTIONS The Standard C++ library contains a set of predefined functions that operate on C style strings. These functions are defined in the header file: cstring Commonly used string functions: strlen(s1) -- returns string length of s1 strcpy(s1,s2) -- copies s2 into s1 strcat(s1, s2) -- concatenates s1 and s2 strcmp(s1, s2) --compares returns -1, 0 or +1 7
Comparing strcmp(a,b) -1 when a <b 0 when a==b 1 when a>b Same as dictionary sort. Early words are "less than" later words. "apple" < "toast"
Example: C Style Strings #include <iostream> #include <cstring> //strcmp(), strcpy(), strcat() uses namespace std; int main(){ char str1[30] = "John", str2[30] = "Joseph"; char phrase[20] = "'s shirt was green", sentence[30]; if (strcmp(str1,str2) < 0) strcpy (sentence, str1); // puts "John" into sentence else strcpy (sentence,str2); strcat(sentence, phrase); cout << "Sentence is: " << sentence << endl; return 0; } //in ch6 cstrings.cpp // puts "Joseph into sentence 9
C-string Length in Copying When using the cpystr() function the length has to be controlled. copycstring.cpp
functions defined in string THE STRING CLASS 11
The string class The string class implements the concept of a character string. A string object can increase and decrease its size dynamically. Numerous operators and functions are defined in the string class. 12
Questions What is the difference between a cstring and a string? Name some advantages of using a string over a cstring? What function can be used to input strings?
Common Functions Defined in string size( ) empty( ) substr (int start, int len) c_str() 14
Overloaded Operators Defined in string relational operators < > == <= >=!= concatenation + += assignment = 15
Example: string class #include <iostream> #include <string> using namespace std; int main(){ string str1 = "John", str2 = "Johnson"; string phrase = "'s shirt was green", sentence; if (str1 < str2) sentence = str1; // assign "John" to sentence else sentence = str2; // assign "Johnson to sentence sentence += phrase; // append phrase to sentence cout << "Sentence is: " << sentence << endl; return 0; } string.cpp 16
Another Example winner.cpp getline(cin, str); //String version of getline
Count the e's Write a program to count the number of e's in an input string. 1)Input the string. 2)len = length of the string 3)For (i = 1 to len) a)if the ith character = 'e' count++ 4.) Output count
Count the e's countes.cpp