MANIPULATION STRINGS IN
C++
Presented by
D.Seethalakshmi MCA.,M.Phil.,
Assistant Professor
Bon Secours College for Women
Thanjavur
Introduction
• A String is a sequence of characters.
• In C++ String object used like any other built-in data types.
• In C++ string is treated as another container class.
• For using the string class must include <string>.
• String class includes
• Constructors
• Member Functions
• Operators
String class achieve the following
• Creating string objects
• Reading string objects from keyboard
• Displaying string objects to the screen
• Finding a substring from a string
• Modifying, Adding and Comparing string objects
• Accessing characters in a string
• Obtaining the size of strings
• ……
String Constructors
• String(); - for creating an empty string
• String(const char * str) - for creating string object from a null-
terminated string
• String(const string & str) - for creating string object from other string
object
String Class Functions
• Append() - appends a part of string to another string
• Assign() - assigns a partial string
• capacity() - gives the total elements that can be used
• Compare() - compares the string against the invoking string
• Erase() - removes characters as specified
• insert() - inserts characters as specified
• length() - gives the number of elements in a string
• replace() - replace specified characters with a given string
• Resize() - changes the size of the string as specified
• Size() - gives the number of characters in the string
• Swap() - swaps the given string with the invoking string
Operators for string objects
operator meaning
= Asssignment
+ Concatenation
+= Concatenation assignment
== Equality
!= Inequality
< Less than
<= Less than or eaual
> Greater than
>= Greater than or equal
[] Subscription
<< Output
>> input
Creating (string) objects
• Can create string objects in a number of ways as
• String s1; //using constructor with no paprameter
• String s2(“abc”); //using one-argument constructor
• S1=s2; //assigning string objects
• S3=“abc”+s3; //concatenating strings
• Cin >> s1; //reading through keyboard ()
• Getline(cin,s2); //reading through keyboard a line of text
•Manipulating string objects
we can easily modify the contents of string objects in several ways, such as
insert(),replace(),erase() and append().
Examples
s1=12345
s2= abcde
Insert()
s1.insert(4,s2); output s1=1234abcde5
Remove()
s1.erase(4,5); output s1=12345
Replace()
s2.replace(1,3,s1); output s2=a12345e
Relational operations
the relational operators are overloaded and can be used to compare string objects.
Compare()
s1.compare(s2)
String characteristics
the functions are size(),length(),capacity() etc.,
str.size();
str.length();
str.capacity();
str.max_size();
Accessing characters in strings
The string class support following fuctions
at() - for accessing individual characters
substr() - for retrieving a substring
find() - for finding a specified substring
Comparing and swapping
The compare() function used to compare either two strings or portions of strings.
the swap() function used for swapping the content of two string objects.
example
s1.compare(s2);
s1.swap(s2);

Manipulation strings in c++

  • 1.
    MANIPULATION STRINGS IN C++ Presentedby D.Seethalakshmi MCA.,M.Phil., Assistant Professor Bon Secours College for Women Thanjavur
  • 2.
    Introduction • A Stringis a sequence of characters. • In C++ String object used like any other built-in data types. • In C++ string is treated as another container class. • For using the string class must include <string>. • String class includes • Constructors • Member Functions • Operators
  • 3.
    String class achievethe following • Creating string objects • Reading string objects from keyboard • Displaying string objects to the screen • Finding a substring from a string • Modifying, Adding and Comparing string objects • Accessing characters in a string • Obtaining the size of strings • ……
  • 4.
    String Constructors • String();- for creating an empty string • String(const char * str) - for creating string object from a null- terminated string • String(const string & str) - for creating string object from other string object
  • 5.
    String Class Functions •Append() - appends a part of string to another string • Assign() - assigns a partial string • capacity() - gives the total elements that can be used • Compare() - compares the string against the invoking string • Erase() - removes characters as specified • insert() - inserts characters as specified • length() - gives the number of elements in a string • replace() - replace specified characters with a given string • Resize() - changes the size of the string as specified • Size() - gives the number of characters in the string • Swap() - swaps the given string with the invoking string
  • 6.
    Operators for stringobjects operator meaning = Asssignment + Concatenation += Concatenation assignment == Equality != Inequality < Less than <= Less than or eaual > Greater than >= Greater than or equal [] Subscription << Output >> input
  • 7.
    Creating (string) objects •Can create string objects in a number of ways as • String s1; //using constructor with no paprameter • String s2(“abc”); //using one-argument constructor • S1=s2; //assigning string objects • S3=“abc”+s3; //concatenating strings • Cin >> s1; //reading through keyboard () • Getline(cin,s2); //reading through keyboard a line of text
  • 8.
    •Manipulating string objects wecan easily modify the contents of string objects in several ways, such as insert(),replace(),erase() and append(). Examples s1=12345 s2= abcde Insert() s1.insert(4,s2); output s1=1234abcde5 Remove() s1.erase(4,5); output s1=12345 Replace() s2.replace(1,3,s1); output s2=a12345e
  • 9.
    Relational operations the relationaloperators are overloaded and can be used to compare string objects. Compare() s1.compare(s2) String characteristics the functions are size(),length(),capacity() etc., str.size(); str.length(); str.capacity(); str.max_size();
  • 10.
    Accessing characters instrings The string class support following fuctions at() - for accessing individual characters substr() - for retrieving a substring find() - for finding a specified substring Comparing and swapping The compare() function used to compare either two strings or portions of strings. the swap() function used for swapping the content of two string objects. example s1.compare(s2); s1.swap(s2);