string data type
DESCRIPTION
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 andTRANSCRIPT
![Page 1: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/1.jpg)
String data type
Cs 240
![Page 2: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/2.jpg)
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 <<
![Page 3: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/3.jpg)
Character Input
Reading in a character
char ch;cin >> ch; // Reads in any non-blank char
![Page 4: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/4.jpg)
What is a String?
• Generally speaking, a string is a sequence of characters
• Examples:• “hello” • “high school”• “H2O”• “4620000”
![Page 5: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/5.jpg)
5
String type
• To use the data type string, the program must include the header file <string>
#include <string>
![Page 6: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/6.jpg)
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 AliNOTE:Empty string “”
![Page 7: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/7.jpg)
Declaration of stringsstring 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
![Page 8: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/8.jpg)
Declaration of strings
• string str = ‘m’;• string str2 = 22;• BOTH ARE NOT CORRECT, and result in syntax
error!
![Page 9: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/9.jpg)
String Input
Reading in a string objectstring str; cin >> str;
getline(cin, str);
• getline reads till a newline ‘\n’ is encountered.Read from the keyborad
Reads in a string with no blanks (reads one word)
Reads in a string that may contain blanks(Reads entire line)
![Page 10: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/10.jpg)
String Input
cin >> name; Type in “Alice Wonderland” Result: name “Alice”
• Instead use
getline (cin, name); Result: name “Alice
Wonderland”
![Page 11: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/11.jpg)
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;
}
![Page 12: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/12.jpg)
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;
}
![Page 13: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/13.jpg)
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;} }
![Page 14: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/14.jpg)
Word at a Time Input
![Page 15: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/15.jpg)
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;} }
![Page 16: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/16.jpg)
Line at a Time Input
![Page 17: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/17.jpg)
String Assignment
targetString = sourceString;OR
targetString.assign(sourceString);OR
targetString.assign(sourceString,start,numOfCharacters);
![Page 18: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/18.jpg)
String Assignment
![Page 19: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/19.jpg)
String Concatenation
• str1 = str2 + str3;• str1.append(“XXX”);• str1.append(str2);• str1 += str2;
![Page 20: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/20.jpg)
String Concatenation
![Page 21: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/21.jpg)
String SizestrVar.size();
![Page 22: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/22.jpg)
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);
![Page 23: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/23.jpg)
Comparing strings
![Page 24: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/24.jpg)
substrings• str1.substr(startPos,length);
![Page 25: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/25.jpg)
Swapping strings
• str1.swap(str2);
![Page 26: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/26.jpg)
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.
![Page 27: String data type](https://reader035.vdocument.in/reader035/viewer/2022062501/568165d0550346895dd8def8/html5/thumbnails/27.jpg)
Finding substrings in a string