05-String Manipulations Updated On Oct 7 2018
05-String Manipulations Updated On Oct 7 2018
Programming Technique II
(SCSJ1023)
Adapted from Tony Gaddis and Barret Krupnow (2016), Starting out with
C++: From Control Structures through Objects
string firstName;
cout << “Enter your first name : ”;
cin >> firstName;
String Input - example
String input : cin
Example :
getline(cin, name)
String input : getline()
The getline() with 3 parameters
1st and 2nd as the getline() with 2 parameters
3rd specifies the terminator character i.e. the character at which
getline() will stop reading from the input stream
Example :
iFile.open(“String1.dat”);
getline(iFIle, name, ‘\t’);
string operator
string s3, s4;
string s1(“Hi");
string s2(“ Dad");
OPERATOR MEANING
OPERATOR MEANING
Categories
assignment : assign, copy, data
modification : append, clear, erase, insert,
replace, swap
space management : capacity, empty, length,
resize, size
substrings : find, substr
comparison : compare
Example:
string s1 = “UTM Skudai”;
int len = s1.length(); // len is 10
Modification of string objects
Appending strings (to the end of a string)
string str1 = “UTM ”;
string str2 = “Skudai ”;
syntax is :
s.substr(position, length);
Example:
string name = “objects”;
// copy a sub string from name starting at position 2 and taking 3 characters
string sub = name.substr(2, 3); // sub has “jec”