Check string length and compare string values - C++ STL

C++ examples for STL:string

Description

Check string length and compare string values

Demo Code

#include <iostream>
#include <string>
using namespace std;
int main()/* w w  w. j a  va 2 s .c om*/
{
   string string1 = "Hello";
   string string2 = "Hello there";
   cout << "string1 is the string: " << string1 << endl;
   cout << "The number of characters in string1 is " << int(string1.length()) << endl << endl;
   cout << "string2 is the string: " << string2 << endl;
   cout << "The number of characters in string2 is " << int(string2.length()) << endl << endl;
   
   if (string1 < string2)
      cout << string1 << " is less than " << string2 << endl << endl;
   else if (string1 == string2)
      cout << string1 << " is equal to " << string2 << endl << endl;
   else
      cout << string1 << " is greater than " << string2 << endl << endl;
   string1 = string1 + " there world!";
   cout << "After concatenation, string1 contains the characters: " << string1 << endl;
   cout << "The length of this string is " << int(string1.length()) << endl;
   return 0;
}

Result


Related Tutorials