String and Character Manipulation
-
Upload
rinah-jones -
Category
Documents
-
view
39 -
download
2
description
Transcript of String and Character Manipulation
![Page 1: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/1.jpg)
http://cs.mst.edu
String and Character Manipulation
![Page 2: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/2.jpg)
http://cs.mst.edu
C-style Strings (ntcas) char name[10] = “Clayton”;
#include <string> using std::string; ... string name = “Clayton”;
C++ Standard Library Strings(a.k.a. Standard String Class)
![Page 3: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/3.jpg)
http://cs.mst.edu
std::string #include <string> using std::string;
int main() { string name = “Clayton”; cout << name.length();
![Page 4: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/4.jpg)
http://cs.mst.edu
7
![Page 5: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/5.jpg)
http://cs.mst.edu
std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;
![Page 6: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/6.jpg)
http://cs.mst.edu
std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;
User enters: Pattie Boyd
![Page 7: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/7.jpg)
http://cs.mst.edu
std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;
System outputs: Pattie
![Page 8: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/8.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
![Page 9: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/9.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
System outputs: What’s your name?
![Page 10: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/10.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
User enters: James Marshall
![Page 11: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/11.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
System places “James Marshall” into the input buffer
![Page 12: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/12.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
System pulls “James” from the buffer and into name
![Page 13: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/13.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
System outputs: What is your hometown?
![Page 14: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/14.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
System does not wait for the user input
![Page 15: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/15.jpg)
http://cs.mst.edu
std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;
Instead “Marshall” is pulled from the buffer and into hometown
![Page 16: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/16.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
![Page 17: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/17.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
![Page 18: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/18.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
![Page 19: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/19.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
![Page 20: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/20.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
Delimiter Character is NOT included in input;
it’s discarded.
![Page 21: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/21.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
System outputs: What’s your name?
![Page 22: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/22.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
User enters: Janis Joplin
![Page 23: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/23.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
System outputs: What is your hometown?
![Page 24: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/24.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
User enters: Port Arthur
![Page 25: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/25.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
System outputs: Janis Joplin
![Page 26: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/26.jpg)
http://cs.mst.edu
std::string input fix // getline(input_stream,string_var,delimiter_char); string name; string hometown; cout << “What’s your name? ”; getline(cin, name, ‘\n’); cout << “What is your hometown?”; getline(cin, hometown, ‘\n’); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
System continues output: Janis JoplinPort Arthur
![Page 27: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/27.jpg)
http://cs.mst.edu
std::string alternative fix string name; string hometown; cout << “What’s your name? ”; getline(cin, name); cout << “What is your hometown?”; getline(cin, hometown); cout << “Name: “ << name; cout << “Hometown: “ << hometown;
![Page 28: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/28.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
![Page 29: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/29.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
System outputs: What’s your age?
![Page 30: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/30.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
User enters: 8
![Page 31: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/31.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
System places “8\n” into the input buffer
![Page 32: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/32.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
System pulls “8” from the buffer and into age
![Page 33: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/33.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
System outputs: What’s your name?
![Page 34: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/34.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
System does not wait for the user input
![Page 35: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/35.jpg)
http://cs.mst.edu
std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);
Instead it pulls “\n” from the buffer into name and getline is finished
![Page 36: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/36.jpg)
http://cs.mst.edu
std::string vs C-string(ntca) string sname; const int SIZE = 80; char cname[SIZE];
cout << “What’s your given name? ”; getline(cin, sname); cout << “What’s your family name? ”; cin.getline(cname, SIZE-1); //different syntax!!
![Page 37: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/37.jpg)
http://cs.mst.edu
more C-string getline const int SIZE = 10; char name[SIZE];
cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;
![Page 38: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/38.jpg)
http://cs.mst.edu
more C-string getline const int SIZE = 10; char name[SIZE];
cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;
? ? ? ? ? ? ? ? ? ?
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
![Page 39: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/39.jpg)
http://cs.mst.edu
more C-string getline const int SIZE = 10; char name[SIZE];
cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;
System outputs: What’s your name?
? ? ? ? ? ? ? ? ? ?
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
![Page 40: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/40.jpg)
http://cs.mst.edu
more C-string getline const int SIZE = 10; char name[SIZE];
cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;
System clears any extraneous \n from the input buffer
? ? ? ? ? ? ? ? ? ?
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
![Page 41: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/41.jpg)
http://cs.mst.edu
more C-string getline const int SIZE = 10; char name[SIZE];
cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;
User enters: Chimley
C h i m l e y \0 ? ?
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
![Page 42: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/42.jpg)
http://cs.mst.edu
more C-string getline const int SIZE = 10; char name[SIZE];
cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;
System outputs: Chimley
C h i m l e y \0 ? ?
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
![Page 43: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/43.jpg)
http://cs.mst.edu
Extraction vs. getlineThe extraction operator (cin>>variable) will always skip leading whitespace and leave \n in the buffer.The getline functions will read leading whitespace and will not leave the \n in the buffer.Rule: always know what is in your input buffer.
![Page 44: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/44.jpg)
http://cs.mst.edu
#include<cctype> toupper(char)
returns the uppercase of arg sent toupper('a'); -> 'A‘ tolower(char)
similar isupper(char)
returns bool: true if uppercase isupper('a'); -> false islower(char)
similar isalpha(char)
similar isdigit(char)
similar ispunct(char)
returns bool: true if punctuation ispunct('!'); -> true isspace(char)
returns bool: true if whitespace – space, newline, tab
![Page 45: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/45.jpg)
http://cs.mst.edu
C-string Manipulationint i = 0, count = 0;char ntca[20] = “Hello! Hi.”;
while (ntca[i] != ‘\0’){ if (ispunct(ntca[i])) count++;
i++;}
cout<<count<<endl;
![Page 46: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/46.jpg)
http://cs.mst.edu
2
![Page 47: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/47.jpg)
http://cs.mst.edu
C-string Manipulationint i = 0, count = 0;char ntca[20] = “Hello! Hi.”;
while (ntca[i] != ‘\0’){ if (isspace(ntca[i])) count++;
i++;}
cout<<count<<endl;
![Page 48: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/48.jpg)
http://cs.mst.edu
1
![Page 49: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/49.jpg)
http://cs.mst.edu
C-string Manipulationint i = 0, count = 0;char ntca[20] = “Hello! Hi.”;
while (ntca[i] != ‘\0’){ ntca[i] = toupper(ntca[i]); i++;}
cout<<ntca<<endl;
![Page 50: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/50.jpg)
http://cs.mst.edu
HELLO! HI.
![Page 51: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/51.jpg)
http://cs.mst.edu
Write Your Ownbool IsDigit (const char input)
![Page 52: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/52.jpg)
http://cs.mst.edu
Write Your Ownbool IsDigit (const char input){ bool digit = false; if (input >= 48 && input <= 57) digit = true; return digit;}
![Page 53: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/53.jpg)
http://cs.mst.edu
Write Your Ownbool IsDigit (const char input){ return(input>=48 && input<=57);}
![Page 54: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/54.jpg)
http://cs.mst.edu
Character Input and Output get – allows one character to be read from input
cin.get(char_variable); peek – reads the next character from the input buffer
without extracting it char_variable = cin.peek();
putback – places the character back into the input buffer cin.putback(char_variable);
put – outputs a single character cout.put(char_variable) equivalent to cout << char_variable
![Page 55: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/55.jpg)
http://cs.mst.edu
getchar next;cout<<”enter your poem: “;do{ cin.get(next); cout<<next;} while (next != ‘\n’);
![Page 56: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/56.jpg)
http://cs.mst.edu
getchar next;cout<<”enter your poem: “;do{ cin.get(next); cout<<next;} while (next != ‘\n’);
char poetry[500];cout<<”enter your poem: “;cin.getline(poetry, 499);cout<<poetry;
getlinevs.
![Page 57: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/57.jpg)
http://cs.mst.edu
Input Options getline()
reads line-by-line cin >>
reads word-by-word get()
reads character-by-character
![Page 58: String and Character Manipulation](https://reader036.fdocuments.net/reader036/viewer/2022062314/5681384d550346895d9ff857/html5/thumbnails/58.jpg)
http://cs.mst.edu
End of Session