C++ String Class and its Applications | Set 2

2.1

We have discussed String class and some of its functions in below set 1.

C++ string class and its applications | Set 1

More functions are discussed in this article

String vs Character Array
In C++, in addition to character array, there exist a similar kind of way to implement string, that is using string class which is a part of C++ standard library. To implement string using string class, we need to add header file . Basic difference between character array and a string is, in case of character array, the size has to be alloted at the time of declaration, i.e all memory once allocated is fixed and cannot be altered at run time. Whereas, for string, there is no need to specify the size and to allocate fixed memory at the time of declaration.

// C++ program to demonstrate Character Array
// and String
#include<iostream>
#include<string>// for string class
using namespace std;
int main()
{
    // Size has to be predefined in character array
    char str[80] = "GeeksforGeeks";

    // Size not predefined in string
    string s("GeeksforGeeks");

    // Printing character array and string
    cout << str << endl;
    cout << s << endl;

    return 0;
}

Output:

GeeksforGeeks
GeeksforGeeks

Some useful String Functions

  • compare(string_to_compare ) :- It is used to compare two strings. It returns the difference of second string and first string in integer.
    // C++ program to demonstrate use of compare()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        string str("GeeksforGeeks");
        string str1("GeeksforGeeks");
    
        // Comparing strings using compare()
        if ( str.compare(str1) == 0 )
            cout << "Strings are equal";
        else 
            cout << "Strings are unequal";
        return 0;
    }
    

    Output :

    Strings are equal
  • find(“string”): Searches the string for the first occurrence of the substring specified in arguments. It returns the position of the first occurrence of substring.
  • find_first_of(“string”): Searches the string for the first character that matches any of the characters specified in its arguments. It returns the position of the first character that matches.
  • find_last_of(“string”): Searches the string for the last character that matches any of the characters specified in its arguments. It returns the position of the last character that matches.
  • rfind(“string”): Searches the string for the last occurrence of the substring specified in arguments. It returns the position of the last occurrence of substring
    // C++ program to demonstrate working of find(),
    // rfind(),find_first_of() and find_last_of()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        string str("The Geeks for Geeks");
    
        // find() returns position to first
        // occurrence of substring "Geeks"
        // Prints 4
        cout << "First occurrence of \"Geeks\" starts from : ";
        cout << str.find("Geeks") << endl;
    
        // Prints position of first occurrence of
        // any character of "reef" (Prints 2)
        cout << "First occurrence of character from \"reef\" is at : ";
        cout << str.find_first_of("reef") << endl;
    
        // Prints position of last occurrence of
        // any character of "reef" (Prints 16)
        cout << "Last occurrence of character from \"reef\" is at : ";
        cout << str.find_last_of("reef") << endl;
    
        // rfind() returns position to last
        // occurrence of substring "Geeks"
        // Prints 14
        cout << "Last occurrence of \"Geeks\" starts from : ";
        cout << str.rfind("Geeks") << endl;
    
        return 0;
    
    }
    

    Output:

    First occurrence of "Geeks" starts from : 4
    First occurrence of character from "reef" is at : 2
    Last occurrence of character from "reef" is at : 16
    Last occurrence of "Geeks" starts from : 14
    
  • insert(pos_to_begin,string_to_insert): This function inserts the given substring in the string. It takes two arguments, first the position from which you want to insert the substring and second the substring.
    // C++ program to demonstrate working of insert()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        string str("Geeksfor");
    
        // Printing the original string
        cout << str << endl;
    
        // Inserting "for" at 5th position
        str.insert(8,"Geeks");
    
        // Printing the modified string
        // Prints "GeeksforGeeks"
        cout << str << endl;
    
        return 0;
    }
    

    Output:

    Geeksfor
    GeeksforGeeks
    
  • clear(): This function clears all the characters from the string. The string becomes empty (length becomes 0) after this operation.
  • empty(): Tests whether the string is empty. This function return a Boolean value.
    // C++ program to demonstrate working of clear()
    // and empty()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        string str("GeeksforGeeks");
    
        // clearing string
        str.clear();
    
        // Checking if string is empty
        (str.empty()==1)?
             cout << "String is empty" << endl:
             cout << "String is not empty" << endl;
    
        return 0;
    
    }
    

    Output:

    String is empty
    

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.1 Average Difficulty : 2.1/5.0
Based on 17 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.