1 / 58

String and Character Manipulation

String and Character Manipulation. C-style Strings ( ntcas ). C++ Standard Library Strings (a.k.a. Standard String Class). char name[10] = “Clayton”; #include <string> using std::string; ... string name = “Clayton”;. std::string. #include <string> using std::string;

cormac
Download Presentation

String and Character Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String and Character Manipulation

  2. C-style Strings (ntcas) C++ Standard Library Strings (a.k.a. Standard String Class) char name[10] = “Clayton”; #include <string> using std::string; ... string name = “Clayton”;

  3. std::string #include <string> using std::string; int main() { string name = “Clayton”; cout << name.length();

  4. 7

  5. std::string input limitations string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;

  6. std::string input limitations User enters: Pattie Boyd string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;

  7. std::string input limitations System outputs: Pattie string name; cout << “What’s your name? ”; cin >> name; cout << name << endl;

  8. std::string input limitations string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  9. std::string input limitations System outputs: What’s your name? string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  10. std::string input limitations User enters: James Marshall string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  11. std::string input limitations System places “James Marshall” into the input buffer string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  12. std::string input limitations System pulls “James” from the buffer and into name string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  13. std::string input limitations System outputs: What is your hometown? string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  14. std::string input limitations System does not wait for the user input string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  15. std::string input limitations Instead “Marshall” is pulled from the buffer and into hometown string name; string hometown; cout << “What’s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown;

  16. 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;

  17. 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;

  18. 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;

  19. 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;

  20. std::string input fix Delimiter Character is NOT included in input; it’s discarded. // 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;

  21. std::string input fix System outputs: What’s your name? // 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;

  22. std::string input fix User enters: Janis Joplin // 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;

  23. std::string input fix System outputs: What is your hometown? // 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;

  24. std::string input fix User enters: Port Arthur // 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;

  25. std::string input fix System outputs: Janis Joplin // 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;

  26. std::string input fix System continues output: Janis JoplinPort Arthur // 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;

  27. 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;

  28. std::string more input nuances string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  29. std::string more input nuances System outputs: What’s your age? string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  30. std::string more input nuances User enters: 8 string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  31. std::string more input nuances System places “8\n” into the input buffer string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  32. std::string more input nuances System pulls “8” from the buffer and into age string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  33. std::string more input nuances System outputs: What’s your name? string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  34. std::string more input nuances System does not wait for the user input string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  35. std::string more input nuances Instead it pulls “\n” from the buffer into name and getline is finished string name; int age; cout << “What’s your age? ”; cin >> age; cout << “What’s your name? ”; getline(cin, name);

  36. std::string vsC-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!!

  37. 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;

  38. more C-string getline [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ? ? ? ? ? ? ? ? ? ? const int SIZE = 10; char name[SIZE]; cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;

  39. more C-string getline System outputs: What’s your name? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ? ? ? ? ? ? ? ? ? ? const int SIZE = 10; char name[SIZE]; cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;

  40. more C-string getline System clears any extraneous \n from the input buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ? ? ? ? ? ? ? ? ? ? const int SIZE = 10; char name[SIZE]; cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;

  41. more C-string getline User enters: Chimley [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] C h i m l e y \0 ? ? const int SIZE = 10; char name[SIZE]; cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;

  42. more C-string getline System outputs: Chimley [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] C h i m l e y \0 ? ? const int SIZE = 10; char name[SIZE]; cout << “What’s your name? ”; cin.ignore(500, ‘\n’); cin.getline(name, SIZE-1); cout << name << endl;

  43. Extraction vs. getline The 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.

  44. #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

  45. C-string Manipulation inti = 0, count = 0; char ntca[20] = “Hello! Hi.”; while (ntca[i] != ‘\0’) { if (ispunct(ntca[i]))         count++; i++; } cout<<count<<endl;

  46. 2

  47. C-string Manipulation inti = 0, count = 0; char ntca[20] = “Hello! Hi.”; while (ntca[i] != ‘\0’) { if (isspace(ntca[i]))         count++; i++; } cout<<count<<endl;

  48. 1

  49. C-string Manipulation inti = 0, count = 0; char ntca[20] = “Hello! Hi.”; while (ntca[i] != ‘\0’) { ntca[i] = toupper(ntca[i]); i++; } cout<<ntca<<endl;

  50. HELLO! HI.

More Related