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

Different Ways To Define Object

The standard C++ string class provides string manipulation operations like copying, searching, and replacing. It allows overloaded operators like + and =. Strings can be defined in different ways like "string str;" or "string str = "Man";". Functions like getline() and >> operator allow reading strings from the keyboard. Common string functions include length(), empty(), assign(), compare(), append(), substr(), and find().

Uploaded by

Muhammad Sarmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Different Ways To Define Object

The standard C++ string class provides string manipulation operations like copying, searching, and replacing. It allows overloaded operators like + and =. Strings can be defined in different ways like "string str;" or "string str = "Man";". Functions like getline() and >> operator allow reading strings from the keyboard. Common string functions include length(), empty(), assign(), compare(), append(), substr(), and find().

Uploaded by

Muhammad Sarmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

THE STANDARD C++ STRING CLASS

• It provides a number of string manipulation operations


such as copying, searching, replacing etc. “string” class
allows the use of overloaded operators (e.g. +, +=, = =).

DIFFERENT WAYS TO DEFINE OBJECT:


• string str;
• string str (“Man”);
• string str = “Man”;
• string str(8, ‘x’);
[string containing eight ‘x’ characters].
• The stream extraction operator (>>) is also overloaded to support
strings. The statement

string str;
cin >> str;

reads a string from the keyboard. Input is delimited by white-space


characters. When a delimiter is encountered, the input operation is
terminated.

• Function getline is also overloaded for strings. The statement


string str;
getline (cin,str);

reads a string from the keyboard. getline can read a line of text and
STRING FUNCTIONS:

• getline (cin, str); getline(cin, str, delim);


Reads a string from keyboard

• str.length( ); / str.size( );
Returns the size/length of the string str.

• str.empty ( );
Informs if the string is empty or not. Returns true (1) or false (0).
STRING FUNCTIONS (Contd.)
ASSIGNMENT AND CONCATENATION:

 str1.assign (str2); (Alternative str1=str2;)


Assigns the value of str2 to str1

 str [2] = ‘r’;


Assigns value ‘r’ to the 2nd index of string str.

 str.at (4);
Returns character at 4th index of string str.

 str.append (“bomb”); (Alternative str += “bomb”;)


Append string str and “bomb”.

 str.append (str1, StartIndex, Length);


Appends string str1 with string str.
STRING FUNCTIONS (Contd.)
COMPARING STRINGS:

• str1.compare(str2);
Compares string str2 with string str1. Returns an integer value.
If both are equal returns 0. Returns a positive number if string str1 is greater, while
returns negative number if string str2 is greater than string str1.
(str1 == str2), (str1 < str2), (str1 > str2)

• str1.compare (StartIndex, Length, str2, StartIndex, Length);


Compares a part of string str1 with a part of string str2. Returns an integer value.

• str1.compare(StartIndex, LengthOfIndex, str2);


Compares a part of string str1 with string str2. Returns an integer value.
ASSIGNMENT:

• Define two strings str1, str2.


• Check if both strings are empty.
• Assign values “Amjad Khan” & “ studying at
University” to string str1 and str2 respectively.
• Check length of both strings.
• Print the character on index 2.
• Check if str1 is greater than str2 and vice versa.
• Append “ is” at the end of str1.
• Replace character ‘d’ with ‘q’.
• Append str1 with str2.
STRING FUNCTIONS (Contd.)
SUBSTRING:

• str1=str2.substr (StartIndex, LengthOfIndex);


Retrieves a substring of a string str2.

SWAPING STRING:

• str1.swap(str2);
Swaps the values of string str1 and str2.
STRING FUNCTIONS (Contd.)
FINDING STRINGS & CHARACTERS IN A STRING:

• str.find (“is”);
Returns the location (starting index) of the string “is” in a string str. (Forward Search)

• str.find (“is”, position+1);


Returns the location (starting index) of the string “is” in a string str. (Forward Search). Function
takes two arguments: The string to search and the index from where search must start.

• str.rfind (“is”);
Returns the location (starting index) of the string “is” in a string str. (Backward Search)

• str.find_first_of(“abcd”);
Locates the first occurrence in the string str of any character in “abcd”.

• str.find_last_of(“abcd”);
Locates the last occurrence in the string str of any character in “abcd”.
STRING FUNCTIONS (Contd.)
REPLACING CHARACTERS IN A STRING:

• str.replace (index, 3, “The”);


Used to replace the character in a string. Function takes three
arguments: the location in the string at which replacement should
begin, the number of characters to replace and the replacement
string.

• str.erase (6);
Used to erase everything from (and including) the character in
position 6 to the end of string str.

• str.erase(5 , 8);
Used to erase from (and including) the character in position 5 till the
next 7 characters. (Index 5-12)

You might also like