Different Ways To Define Object
Different Ways To Define Object
string str;
cin >> str;
reads a string from the keyboard. getline can read a line of text and
STRING FUNCTIONS:
• 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:
str.at (4);
Returns character at 4th index of string str.
• 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)
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.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.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)