MANIPULATION STRINGS IN C Presented by D Seethalakshmi

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 nullterminated 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() 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 s 1; //using constructor with no paprameter String s 2(“abc”); //using one-argument constructor S 1=s 2; //assigning string objects S 3=“abc”+s 3; //concatenating strings Cin >> s 1; //reading through keyboard () Getline(cin, s 2); //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 s 1=12345 s 2= abcde Insert() s 1. insert(4, s 2); output s 1=1234 abcde 5 Remove() s 1. erase(4, 5); output s 1=12345 Replace() s 2. replace(1, 3, s 1); output s 2=a 12345 e

Relational operations the relational operators are overloaded and can be used to compare string objects. Compare() s 1. compare(s 2) 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 s 1. compare(s 2); s 1. swap(s 2);
- Slides: 10