string and character manipulation
play

String and Character Manipulation http://cs.mst.edu C-style Strings - PowerPoint PPT Presentation

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


  1. String and Character Manipulation http://cs.mst.edu

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

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

  4. http://cs.mst.edu

  5. std::string input limitations string name; cout << “What‟s your name? ”; cin >> name; cout << name << endl; http://cs.mst.edu

  6. std::string input limitations string name; cout << “What‟s your name? ”; cin >> name; cout << name << endl; User enters: Pattie Boyd http://cs.mst.edu

  7. std::string input limitations string name; cout << “What‟s your name? ”; cin >> name; cout << name << endl; System outputs: Pattie http://cs.mst.edu

  8. std::string input limitations string name; string hometown; cout << “What‟s your name? ”; cin >> name; cout << “What is your hometown?”; cin >> hometown; http://cs.mst.edu

  9. 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? http://cs.mst.edu

  10. 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 http://cs.mst.edu

  11. 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 http://cs.mst.edu

  12. 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 http://cs.mst.edu

  13. 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? http://cs.mst.edu

  14. 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 http://cs.mst.edu

  15. 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 http://cs.mst.edu

  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; http://cs.mst.edu

  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; http://cs.mst.edu

  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; http://cs.mst.edu

  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; http://cs.mst.edu

  20. 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. http://cs.mst.edu

  21. 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? http://cs.mst.edu

  22. 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 http://cs.mst.edu

  23. 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? http://cs.mst.edu

  24. 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 http://cs.mst.edu

  25. 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 http://cs.mst.edu

  26. 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 http://cs.mst.edu

  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; http://cs.mst.edu

  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); http://cs.mst.edu

  29. 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? http://cs.mst.edu

  30. 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 http://cs.mst.edu

  31. 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 http://cs.mst.edu

  32. 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 http://cs.mst.edu

  33. 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? http://cs.mst.edu

  34. 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 http://cs.mst.edu

Recommend


More recommend