1 / 27

String data type

String data type. Cs 240. Char & string. char : holds a single character string : holds a sequence of characters Both can be used in assignment statements Both can be displayed with cout and <<. Character Input. Reading in a character char ch ;

sibyl
Télécharger la présentation

String data type

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 data type Cs 240

  2. Char & string • char: holds a single character • string: holds a sequence of characters • Both can be used in assignment statements • Both can be displayed with cout and <<

  3. Character Input Reading in a character char ch; cin >> ch; // Reads in any non-blank char

  4. What is a String? • Generally speaking, a string is a sequence of characters • Examples: • “hello” • “high school” • “H2O” • “4620000”

  5. String type • To use the data type string, the program must include the header file <string> #include <string>

  6. Declaration of strings • string name; //declare and initialize to empty string • string name = “Sara”; //declare and initialize to Sara • string name(“Sara”); //declare and initialize to Sara • string name = “Sara Ali”; //declare and initialize to Sara Ali • string name(“Sara Ali”); //declare and initialize to Sara Ali NOTE: Empty string  “”

  7. Declaration of strings string name= “Sara Ahmad"; declares name to be a string variable and also initializes name to “Sara Ahmad" • The first character in name, ‘S', is in position 0, the second character, ‘a', is in position 1, and so on • The variable name is capable of storing any size string

  8. Declaration of strings • string str = ‘m’; • string str2 = 22; • BOTH ARE NOT CORRECT, and result in syntax error!

  9. String Input Reading in a string object string str; cin >> str; getline(cin,str); • getline reads till a newline ‘\n’ is encountered. Reads in a string with no blanks (reads one word) Reads in a string that may contain blanks(Reads entire line) Read from the keyborad

  10. String Input cin >> name; Type in “Alice Wonderland” Result: name  “Alice” • Instead use getline (cin, name); Result: name  “Alice Wonderland”

  11. String input #include<iostream> #include<string> using namespace std; int main() { string name; cout<<"Enter you name : "; cin>>name; cout<<"Hello "<<name<<endl; return 0; }

  12. String input #include<iostream> #include<string> using namespace std; int main() { string name; cout<<"Enter you name and your family name : "; cin>>name; cout<<"Hello "<<name<<endl; return 0; } #include<iostream> #include<string> using namespace std; int main() { string name; cout<<"Enter you name and your family name : "; getline(cin,name); cout<<"Hello "<<name<<endl; return 0; }

  13. Word at a Time Input #include <iostream> #include <string> using namespace std; int main() { string str1; while (true) { cout << "Enter a string "; cin >> str1; cout << "You entered: " << str1; cout << endl; } }

  14. Word at a Time Input

  15. Line at a Time Input #include <iostream> #include <string> using namespace std; int main() { string str1; while (true) { cout << "Enter a string "; getline(cin,str1); cout << "You entered: " << str1; cout << endl; } }

  16. Line at a Time Input

  17. String Assignment targetString = sourceString;OR targetString.assign(sourceString);OR targetString.assign(sourceString,start,numOfCharacters);

  18. String Assignment

  19. String Concatenation • str1 = str2 + str3; • str1.append(“XXX”); • str1.append(str2); • str1 += str2;

  20. String Concatenation

  21. String Size strVar.size();

  22. Comparing strings • Using Relational and equality operator: == != > < • str1.compare(str2); return 0 if str1 and str2 are equalsreturn + number if str1 > str2return – number if str1 < str2 • str1.compare(pos1,n1,str2); • str1.compare(pos1,n1,str2,pos2,n2);

  23. Comparing strings

  24. substrings • str1.substr(startPos,length);

  25. Swapping strings • str1.swap(str2);

  26. Finding substrings in a string • str1.find(str2);attempts to find str2 in str1.if str2 is found, the subscript of the starting location of that string is returned.If str2 is not found, the value string::npos is returned. • str1.rfind(str2);search backward (right-to-left) if str2 is found, the subscript of the starting location of that string is returned.If str2 is not found, the value string::npos is returned.

  27. Finding substrings in a string

More Related