Manipulation of Strings in C++
A String in C++ is a sequence of characters handled using the string class, offering various functionalities like creating, reading, displaying, modifying, and comparing string objects. This guide covers string constructors, member functions, operators, and ways to manipulate strings effectively in C++. Learn about creating string objects, accessing characters, obtaining string sizes, and more.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
MANIPULATION STRINGS IN C++ Presented by D.Seethalakshmi Assistant Professor Bon Secours College for Women Thanjavur MCA.,M.Phil.,
Introduction AString 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() Assign() capacity() Compare() Erase() insert() length() replace() Resize() Size() Swap() - - - - - - - - - - - appends a part of string to another string assigns a partial string gives the total elements that can be used compares the string against the invoking string removes characters as specified inserts characters as specified gives the number of elements in a string replace specified characters with a given string changes the size of the string as specified gives the number of characters in the string 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; 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 //using constructor with no paprameter
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() substr() find() - - - for accessing individual characters for retrieving a substring 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);