Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with ANSI C ++

Similar presentations


Presentation on theme: "Programming with ANSI C ++"— Presentation transcript:

1 Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

2 Chapter 15 ANSI String Objects

3 The need for string objects: Limitation of character arrays
Character arrays can not be compared like other variables. we can not write String1= = String2 to compare two strings They can not be assigned like normal variables; we can not write String1 = String2 to assign String1 the same value as String2

4 The need for string objects: Limitation of character arrays
C string functions operate on character pointers. They can malfunction in some cases. Ex. strlen function would return a wrong value if null character not present at the end of the character array

5 Operations possible on String Objects
Creating strings Defining a string object in a normal way string FirstString; Defining a string object using initialization string ThirdString(SecondString); Defining a string object using a constructor string SecondString("Hi")

6 Substring operations Find location where specific substring or a character is in a given string Find the character at given location in a given string Insert specific substring at specific place Replacing specific characters by other characters Append substring to string

7 Applying substring operations
string FirstString = "How Are You?"; string SecondString("Hi"); cout << FirstString.find('A')<< endl; // returns where A appears in the string cout << FirstString.at(i); FirstString.insert(4, "busy "); cout << FirstString<< endl; How busy Are You?

8 Applying substring operations
SecondString.append(" friend!"); cout << SecondString << endl; Hi friend! FirstString.replace(4,4, "lazy"); // busy with lazy cout << FirstString << endl; How lazy Are You?

9 Applying substring operations
FirstString.erase(4,5); // now removing lazy cout << FirstString << endl; How Are You?

10 Applying substring operations
operators < , > , <= and >= is used to check if one string is lexicographically greater then (in the order of alphabets like book library order) the other etc. << and >> operators for reading and writing string objects. The [] operator is also available to access an element (a char) from the string object.

11 Operations involving multiple strings
= to assign one string object to another + to concatenate two strings into one = = operator to compare two string objects Use compare function which works like strcmp st1.swap(st2) for swapping st1 with st2 != is used for checking if two strings are not equal. FirstString.length() returns the length of the string in elements.

12 Finding out characteristics of string
st1.empty() to check if st1 is empty string object. st1.size() for string object st1 returns the size of the string st1.max_size() for string object st1 returns maximum size a string in given system can have resize() resize the string by the number supplied as an argument.

13 Other generic algorithms from STL are applicable
find() replace() merge() sort() And few others are available

14 Comparison with C Strings
The string objects of C++ are far more near to actual strings then the C strings. feel of working with string data type rather then working with array of characters. The ease of use and applicability is far more improved. Designers took care that C++ strings are as efficient as C strings


Download ppt "Programming with ANSI C ++"

Similar presentations


Ads by Google