0% found this document useful (0 votes)
357 views

String Handling in C++

Strings are sequences of characters that can be manipulated using C++ string library functions. Key string operations include concatenation to combine strings, comparisons to check alphabetical order, and extracting and modifying substrings. The string class supports indexing individual characters as well as functions for getting the length, checking if a string is empty, and inserting, deleting, or extracting substrings.

Uploaded by

Steven Price
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
357 views

String Handling in C++

Strings are sequences of characters that can be manipulated using C++ string library functions. Key string operations include concatenation to combine strings, comparisons to check alphabetical order, and extracting and modifying substrings. The string class supports indexing individual characters as well as functions for getting the length, checking if a string is empty, and inserting, deleting, or extracting substrings.

Uploaded by

Steven Price
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

String handling

Definition of Strings
a string is a sequence of characters
Examples: hello, high school, H2O.
Typical desirable operations on strings are:
Concatenation: high+school=highschool
Comparisons: high<school // alphabetical
Finding/retrieving/modifying/deleting/inserting substrings in a given
string
C++ has a <string> library
Include it in your programs when you wish to use strings: #include <string>
In this library, a class string is defined and implemented
It is very convenient and makes string processing easier than in C
The following instructions are all equivalent. They declare x to be an object
of type string, and assign the string high school to it:
string x(high school);
string x= high school;
string x; x=high school;
Operations on strings (Concatenation)
Let x and y be two strings
To concatenate x and y, write: x+y
string x= high;
string y= school;
string z;
z=x+y;
cout<<z=<<z<<endl;
z =z+ was fun;
cout<<z=<<z<<endl;
Output:
z=highschool
z= highschool was fun
Concatenation of Mixed-Style Strings
In s=u+v+w; where s is of type string,
u can be
A string object, or
a C-style string (a char array or a char pointer),
a C-style char
or a double-quoted string,
or a single-quoted character.
Same with v and w.
At least u or v or w must be a string object
Eg
string x= high;
char y[]= school;
char z[]= {w,a,s,\0};
char *p = good;
string s= x+y+ +z+ very+ +p+!;
cout<<s=<<s<<endl;
cout<<s=+s<<endl;
Output:
s=highschool was very good!
s=highschool was very good!
Comparison Operators for string Objects
We can compare two strings x and y using the following operators: ==, !=, <,
<=, >, >=
The comparison is alphabetical
The outcome of each comparison is: true or false
The comparison works as long as at least x or y is a string object. The other
string can be a string object, a C-style string variable, or a double-quoted
string.
Eg
string x= high;
char y[]= school;
char *p = good;
If (x<y)
cout<<x<y<<endl;
If (x<tree)
cout<<x<tree<,endl;
If (low != x)
cout<<low != x<<endl;
if( (p>x)
cout<<p>x<<endl;
Else
cout<<p<=x<<endl
Output:
x<y
x<tree
low != x
p>x
The Index Operator []
If x is a string object, and you wish to obtain the value of the k-th character in the
string, you write: x[k];
string x= high;
char c=x[0]; // c is h
c=x[1]; // c is i
c=x[2]; // c is g
This feature makes string objects appear like arrays of chars.
Getting a string Object Length & Checking for Emptiness
To obtain the length of a string object x, call the method length() or size():
int len=x.length( );
--or--
int len=x.size( );
To check of x is empty (that is, has no characters in it):
bool x.empty();
Obtaining Substrings of Strings
Logically, a substring of a string x is a subsequence of consecutive
characters in x
For example, rod is a substring of product
If x is a string object, and we want the substring that begins at position pos
and has len characters (where pos and len are of type int), write:
string y = x.substr(pos,len);
Inserting a String Inside Another
Suppose x is a string object, and let y be another string to be inserted at
position pos of the string of x
To insert y, do: x.insert(pos,y);
The argument y can be: a string object, a C-style string variable, or a
double-quoted string
Deleting (Erasing) a Substring of a string Object
Suppose x is a string object, and suppose you want to delete/erase the
characters in the range [pos,pos+len) in x.
x.erase(pos,len);
To erase the whole string of x, do: x.clear( );

You might also like