






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to create and manipulate character string data in c++ using both cstrings and the c++ string class. It covers declaring and initializing string variables, concatenation, input/output, and converting between string objects and cstrings. It also introduces the vector class.
Typology: Assignments
1 / 38
This page cannot be seen from the preview
Don't miss anything!































Strings
Read: Chapter 8.
Q: How can we create and manipulate variables holding character string data?
Two Ways:
cstrings in C++:
Syntax in Declaring a cstring Variable: char array_name[max_cstringSize + 1];
This declaration will create a cstring variable (an array of char) that is capable of storing a string with at most max_cstringSize characters.
Initializing cstring Variable during Declaration: Consider char s[10] = “Hi Mom!”;
The array elements are:
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] H i M o M! \0??
Remarks:
Q: What if the max_cstringSize is not specified?
Example: char q[] = “jrm” // q has 4 characters
The array elements are: q[0] q[1] q[2] q[3] *j r m *
Example: The following loop will change a cstring, our_string, into a cstring having the same length but with characters all ‘X’:
int index = 0; while (our_string[index] != '\0') { our_string[index] = 'X'; index++; }
Remark: In the loop above, if our_string has no null terminator, the loop will run off into memory, happily writing on every byte in memory beyond the end of our_string until a byte is found with zero value.
Better using: int index = 0; while (our_string[index] != '\0' && index < size) { our_string[index] = 'X'; index++; }
Assignment Operator in cstring: Warning: The assignment operator does not work for cstrings. Hence, the following are illegal in C++. char s[10]; s = “Hi Mom!”;
However, during declaration, it is legal to use: char s[10] = “Hi Mom!”;
To assign a value to a csting variable, use strcopy function defined in the cstring library.
Example: #include
Warning: In C++, strcpy function does not check the declared length of the first argument. Hence, it is possible for strcpy to write characters beyond the declared size of the array!
Remedy: Many versions of C++ have a safer version of strcpy named strncpy, which includes a third argument representing the maximum number of characters to copy.
Comparing cstrings: The “==” comparison operator does not work as expected with cstrings. To compare two cstring variables, use the predefined function strcmp defined in the cstring library.
Example: #include
Remark: The strcmp function compares the numeric codes of the elements in the C-strings one character at a time.
Example: The following are either disallowed or they don’t do what you would expect: void doThings( ) { char myString[5]; myString = “abc”; char stringA[] = “Hello”; char stringB[] = stringA; if (stringB == “Hello”) cout << “Hello\n”; }
Correct Way: #include
Passing cstrings as Arguments and Parameters: Since cstring variables are arrays, cstring arguments and parameters are used just like array variables.
Example: Replace all vowels in a string with an underscore character. void replaceVowels(char str[ ]) // we don’t pass a length { int i = 0; while (str[i] != ‘\0’) { switch (str[i]) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: case ‘A’: case ‘E’: case ‘I’: case ‘O’: case ‘U’: str[i] = ‘_’; break; }
i++; } }
void tryIt( ) { char command[ ] = “Let’s eat apples”; replaceVowels(command); cout << command << endl; }
Remarks:
Consider the code: char a[80], b[80]; cin >> a >> b ; cout << a << b << “END OF OUTPUT\n”;
Dialogue: Do be do // input DobeEND OF OUTPUT // output
Q: How can we read in an entire line?
Remarks:
The getline Function: Syntax: input_stream.getline(cString, max_char + 1);
One line of input is read and stored in the cstring cString. The getline member function will stop reading when max_char characters have been read.
Example: char a[80]; cin.getline(a, 80); cout << a << “END OF OUTPUT.\n”;
Dialogue: Do be do // input Do be doEND OF OUTPUT // output
Remark: These cstring I/O techniques work the same for file I/O streams.
Q: How do we read the input?
Consider the following function read_and_clean ,
Array of cstrings:
Example: char name[5][20]; // define 5 cstrings of size 19 cout << “Enter 5 names, one per line:\n “; for (int index = 0; index < 5; index++) cin.getline(name[index], 20); for (int index = 0; index < 5; index++) cout << name[index] << endl;
Example: Passing Array of Cstring as Parameter. // Define 10 cstrings, each can hold up to 49 characters char stringArray[10][50]; for (int i=0 ; i<10 ; i++) cin >> stringArray[i]; // read in the string … void lookForBoo(char words[][50], int nWords) { for (int i=0 ; i<nWords ; i++) if (strcmp(words[i], “Boo”) == 0) // Finding “Boo”. cout << “You scared me!\n”; }
Manipulating Character Strings:
Problems with Cstrings:
C++ string Class:
Using Standard String Class in C++: Include Directive: #include