C++ String compare()30 Aug 2024 | 2 min read This function compares the value of the string object to the sequence of characters specified by its parameter. Syntax :Suppose str1 and str2 are two strings and we want to compare these two strings then its syntax would look like:
Example 1Output: Both the strings are unequal Consider two strings str1 and str2. str1 contains value 'Hello' and str2 contains value 'javatpoint' ,We compare these two strings using compare method and this compare method always returns integer value.When we compare these two strings we get the value less than zero.Now,in this case 'if' condition fails then else statement will run and will print "Both the strings are unequal". Example 2Output: Strings are equal Next TopicC++ Strings |
C++ String find_last_of() This function searches the string for the last character that matches any of the character specified in the string. Syntax Consider two strings str1 and str2. Syntax would be : str1.find_last_of(str2); Parameters str : str is the string to be used in searching. pos : It defines the position at...
1 min read
C++ String find_first_not_of() This function is used to search the string for the first character that does not match any of the characters specified in the string. Syntax Consider two strings str1 and str2. Syntax would be : str1.find_first_not_of(str2); Parameters str : str is the string to be used in searching. pos :...
1 min read
C++ String find_last_not_of() This function is used to search the string for the last character that does not match with any of the character specified in the string. Syntax Consider two strings str1 and str2. Its syntax would be : str1.find_last_not_of(str2); Parameters str : str is the string to be used in...
1 min read
C++ String resize() This function is used to resize the string to the length of k characters. Syntax Consider a string object str. To resize the string object, syntax would be : str.resize(k,c); Parameters This function contains two parameters. k : k is number of characters specified in the first parameter. It resizes...
1 min read
C++ String crbegin() The crbegin() function stands for 'reverse beginning'. This function references to the last character of the string. Syntax Consider a string str. Syntax would be: str.crbegin(); Parameter This function does not contain any parameter. Return value This function returns the constant iterator pointing to the last character of the string. Example 1 Let's...
1 min read
C++ String Assign() This functions assigns a new value to the string,replacing all its current contents. Syntax Consider two strings str1 and str2, Syntax would be : Str1.assign(str2); Parameters str : str is a string object, whose value to be assigned. subpos : It defines the position of the character which is to...
1 min read
C++ String insert() This function is used to insert a new character, before the character indicated by the position pos. Syntax Consider two strings str1 and str2, pos is the positon. Syntax would be : str1.insert(pos,str2); Parameters str : String object to be inserted in another string object. pos : It defines the...
2 min read
C++ String back() This function is used to access the last character of string. Syntax Consider a string s. Syntax would be: s.back(); Parameter This function does not contain any parameter. Return value This function returns the reference of last character of the string. Example Let's see the simple example to add a mew character at...
1 min read
C++ String c_str() This function returns a pointer to an array that contains null terminated sequence of characters. Syntax Consider a string str. Syntax would be: str.c_str(); Parameter It does not contain any parameter. Return value It returns a pointer to the c-string representation of the string object's value. Example Let's see the simple example. #include<iostream> #include<cstring> #include<string> using...
1 min read
C++ String size() This function is used to return the length of the string in terms of bytes. It defines the actual number of bytes that conform to the contents of the string object which is not necessarily equal to the capacity. Syntax Consider a string object named...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India