Beruflich Dokumente
Kultur Dokumente
www.lsp4you.com
Manipulating Strings
Learners Support Publications
www.lsp4you.com
Introduction
C ++ does not support a built-in
string type.
We have used null-terminated
character arrays to store and
manipulate strings.
ANSI standard C++ provides a new
class called string to store and
manipulate strings.
Learners Support Publications
www.lsp4you.com
Introduction
Continue…
String Constructors
Commonly used string constructors:
• String ( );
For creating an empty string
• String (const char * str);
For creating a string object from a
null-terminated string
• String (const string & str);
For creating a string object from
other string object
Learners Support Publications
www.lsp4you.com
String Operators
Commonly used string operators:
•=
Assignment
•+
Concatenation
• +=
Concatenation assignment
• ==
Equality
• !=
Inequality
Learners Support Publications
www.lsp4you.com
String Operators
Continue…
• >
Greater than
• >=
Greater than or Equal
• []
Subscription
Learners Support Publications
www.lsp4you.com
String Operators
Continue…
• >>
Input
Learners Support Publications
www.lsp4you.com
• s3 += s1; // s3 = s3 + s1
• s3 += “abc” ; // s3 = s3 + “abc”
Learners Support Publications
www.lsp4you.com
Using cin and >> operator we can read only one word of a
string while the getline( ) function permits us to read a
line of text containing embedded blanks.
Learners Support Publications
www.lsp4you.com
Relational Operations
compare ( ) can be used for comparing
string objects instead of the overloaded
operators.
int x=s1.compare(s2)
if (x == 0)
cout << “s1 is equal to s2”;
else if (x > 0)
cout << “s1 > s2”;
else
cout << “s1 < s2”;
Learners Support Publications
www.lsp4you.com
Thank You