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

String Manipulation

String objects in C++ allow for manipulation of character sequences. Some key capabilities of strings include creating, reading, displaying, finding substrings, modifying, comparing, concatenating, and accessing individual characters of strings. Common string methods include append(), assign(), at(), begin(), capacity(), compare(), empty(), erase(), find(), insert(), length(), replace(), and more. Strings can be created from character literals or user input, manipulated using methods like insert() and erase(), and concatenated using the + operator.

Uploaded by

Prabha Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

String Manipulation

String objects in C++ allow for manipulation of character sequences. Some key capabilities of strings include creating, reading, displaying, finding substrings, modifying, comparing, concatenating, and accessing individual characters of strings. Common string methods include append(), assign(), at(), begin(), capacity(), compare(), empty(), erase(), find(), insert(), length(), replace(), and more. Strings can be created from character literals or user input, manipulated using methods like insert() and erase(), and concatenated using the + operator.

Uploaded by

Prabha Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

String Manipulation

String objects are a special type of container, specifically designed to operate with sequences of characters. ANSI C++ provides a new class called string. For using string class, we must include <string.h> String class are used to achieve the following:

Creating string objects Reading string objects from keyboard Displaying string objects to the screen Finding a substring from a string Modifying string objects Comparing string objects Adding string objects Accessing characters in a string Obtaining size of string

Functions
append() assign() at() begin()

capacity() compare() empty() erase() find() insert() length() replace() ....

Creating string objects:


#include<iostream.h> #include<string.h> void main() { string name; cout << "Enter your name: " << flush; cin >> name; // read string until the next separator // (space, newline, tab) cout<<name; cout<<"\nenter another string including space"; // Or, alternatively: getline (cin, name); // read a whole line into the string name cout<<name; }

Manipulating string objects


string s1(12345); string s2(abcde); cout<<Original strings\n;

cout<<s1=<<s1<<\ns2=<<s2; //inserting a string into another cout<<place s2 inside s1; s1.insert(4,s2);\ cout<<modified string <<s1; //s1=1234abcde5 //removing characters in a string cout<<remove 5 characters from s1; s1.erase(4,5); cout<<now s1=<<s1; //s1=12345 //replace characters in a string cout<<replace middle 3 characters in s2 with s1; s2.replace(1,3,s1); cout<<s2=<<s2; // s2=a12345e

string string string result //

result; s1 = "hello "; s2 = "world"; = s1 + s2; result now contains "hello world"

string result; string s1 = "hello "; string s2 = "world"; result = s1 + s2; // result now contains "hello world"

You might also like