290 likes | 432 Views
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 ;
E N D
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; cin >> ch; // Reads in any non-blank char
What is a String? • Generally speaking, a string is a sequence of characters • Examples: • “hello” • “high school” • “H2O” • “4620000”
String type • To use the data type string, the program must include the header file <string> #include <string>
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 “”
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
Declaration of strings • string str = ‘m’; • string str2 = 22; • BOTH ARE NOT CORRECT, and result in syntax error!
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
String Input cin >> name; Type in “Alice Wonderland” Result: name “Alice” • Instead use getline (cin, name); Result: name “Alice Wonderland”
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; }
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; }
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; } }
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; } }
String Assignment targetString = sourceString;OR targetString.assign(sourceString);OR targetString.assign(sourceString,start,numOfCharacters);
String Concatenation • str1 = str2 + str3; • str1.append(“XXX”); • str1.append(str2); • str1 += str2;
String Size strVar.size();
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);
substrings • str1.substr(startPos,length);
Swapping strings • str1.swap(str2);
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.