C++ String Functions Unleashed: A Practical Guide for Developers[With LeetCode Problems]

In this article, I'm going to cover all the basic string functions that you must know in order to ace your coding. Let's get our hands dirty and look at the most important functions. At the end of this article I have added some bonus in build functions that helped me.

Let's get started.

Getline():

This function is useful when we need to take string input containing space. cin>> is not always enough.

    //GetLine
    string str;

    getline(cin, str);  //Allows you to insert space seprated words for ex: India Lost WTC
    cout << str;

Push_back & Pop_back():

push_back(): It is used to push char to a string,

pop_back: It removes the last char from the string

    Push_back(), Pop_back();

    string str1;
    str1.push_back('I');  //Insert the char I
    cout << str1;
    str1.pop_back();  //Deletes the last char(I here) at the end of string
    cout << str1;

Front(), Back(), At():

Front(): Shows the front char of string

Back(): Shows the last char of string

At(): Returns the index of the specified char

    // Front(), Back(), At()

    string str1 = "Let's see how we can access element in string";

    cout << str1.front()<<" \n"; //Prints the first element
    cout << str1.back()<<" \n";  //Prints the last elemet
    cout << str1.at(6)<<" \n";;  //Return index at specified index

Clear(), Empty(), Erase():

They both have same function that is to remove elements the primary difference is

clear(): clears the entire string i.e make string empty

Erase() : It takes arguments, starting_index & length, thus giving you freedom over what you want to delete

    //Clear()
    string str1 = "Let's clear this string";
    // cout << str1;
    str1.clear();
    cout << str1;
    cout << "The string is empty thus prints nothing"<<endl;
    //Empty(): Checks if string is empty
    if(str1.empty()){
        cout << "TRUE";
    }
    else{
        cout << "FALSE";
    }
    //Erase()
    string str2 = "Let's erase this string from here";
    str2.erase(str2.find("here"),4);  //Removes the word "here"
    cout << str2;

Find():

As the name suggests it's used to find the char/String in given string, it returns NPOS if no such char/string is present.

    //Find() return the starting index of the searched string or Return npos
    string a = "India Lost WTC";
    if(a.find("lost")!=std::string::npos)
        cout << "true"<<a.find("lost");
    else 
        cout << "false";

Problem: https://leetcode.com/problems/remove-all-occurrences-of-a-substring/description/

Compare():

To compare if two string are similar or not

It returns equals to 0 if it's a match, or >0 and <0. You can read more about it on cplusplus link of same is attached below.

    //compare()

    string str1 = "We are comparing strings";
    string str2 = "compare";

    if(str1.compare(str2)==0)
        cout << "True";
    else
        cout << "False";

Problem: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/description/

Substr():

This is the most important function.

   //substr(position to start, length of substring)

    string str1 = "We think in generalities, but we live in details. ";
    int pos = str1.find("but");
    string str2 = str1.substr(0, pos);  
    // o/p: We think in generalities,  
    cout << str2;

Bonus Functions [With LeetCode Questions]:

  1. reverse(): This function can be used to reverse the string

     string s = "Reverse this"
     reverse(s.begin(),s.end())
    

    Problem: https://leetcode.com/problems/reverse-string/

  2. stoi(), to_string():

     //Stoi() string to integer 
     String s = "4";
     int n = stoi(s)
    
     //to_string()
     int a = 45;
     string str = to_string(a);
    
  3. sort()

     string str = "zxcvbnmgfdsa";
     sort(str.begin(),str.end());
    
  4. toupper()

     //ToUpper()
     string s = "lowercase";
     string str = toupper(s);
     cout<<str; // O/p: LOWERCASE 
    
     //ToLower
     string s2 = "UPPERCASE";
     cout<<tolower(s2);
    
  5. tolower()

    Problem: https://leetcode.com/problems/to-lower-case/

  6. next_permutation(str.begin(),str.end())

     int arr[] = [1,2,3];
     cout<<next_permutation(arr,arr+n);
    
     //For vector: next_permutation(v.begin(),v.end());
     //For string: next_permutation(s.begin(),s.end());
    

    Problem: https://leetcode.com/problems/next-permutation/description/

  7. prev_permutation(str.begin(),str.end())

  8. rotate()

    There are two types of rotation Left and Right rotation remember to use + for left and - for r.

    rotate(str.begin(), str.begin()+rotL, str.end());

    rotate(str.begin(), str.begin()-rotR, str.end());

    rotL is the value of no. of time you need to left rotate the string

    rotR is the value of no. of time you need to right rotate the string

Try this rotation question:

Note: take care of the edge cases

Problem: https://leetcode.com/problems/rotate-array/description/

For reference, you can always look up to: cplusplus

Share it, save it and let me know if you love it. This will encourage me to create more posts like this.