Raja001Aug's blog

By Raja001Aug, history, 4 months ago, In English

Strings are one of the most commonly used topics in competitive programming. In this blog, I am sharing my beginner-level notes on strings in C++, covering all the important things I have learned so far. This blog is meant for absolute beginners who are just starting with competitive programming. Let’s start. In competitive programming, we use STL string, not character arrays.
Declaration of a string:
string str_name = "codeforces";

  1. str.length() or str.size() returns the length of a string str.

  2. str.empty() is used to check whether a string is empty.

    if (s.empty())
    cout<<"String is empty";

  3. Input and Output for a single word, We use cin>>str; //(eg. code);
    for a complete sentence We use getline(cin, str); //(eg. Happy New Year)
    To print a string we simply use cout<<str; //for both

  4. Traversing a String
    We can traverse a string like an array or using a range-based loop.
    for(char c : str)
    cout<<c;

  5. To insert a char at the end we use push_back() function (similar to vector)
    str.push_back('x');//add at end

  6. To remove last char we use
    str.pop_back();

  7. String concatenation
    string a = "Thank";
    string b = "You";
    string c = a + " " + b; //Thank you

  8. String Comparison
    string a = "abc", b = "abd";
    if(a == b)
    cout<<"Both are Equal";
    else if(a<b) cout<<"Lexicographically Smaller";
    else if(a>b) cout<<"Lexicographically Larger";

  9. To extract a substring
    string str = "codeforces";
    string sub_str = str.substr(4,6);//forces;
    Format: substr(start_index, length of the substring)//0-based indexing
    Important note: An out-of-range start index causes a runtime error, but if the length exceeds the remaining characters, it
    safely returns the substring till the end.

  10. To find a character or a substring in a string

    int pos = str.find("code");
    if(pos != string::npos)
    cout<<"Found at index "<<pos;
    else
    cout<<"Not found";

  11. To sort a string

    sort(str.begin(), str.end());

  12. To convert character case

    char c = 'A';
    c = tolower(c);//a
    char d = 'b';
    d = toupper(d);//B

    It works on char, not on string. You can use a loop to convert each character of a string.

  13. To store frequency of characters

    vector freq(26, 0);
    for (char c : s)
    freq[c — 'a']++;

  14. To reverse a string

    reverse(str.begin(),str.end());

  15. stoi() is used to convert string into integer

    int num = stoi("143"); //string must contain valid number

  16. to_string() is used to convert integer into a string

    string str = to_string(1432);

  17. Palindrome check technique

    A string is a palindrome if it reads the same forward and backward.

    Code:-
    bool isPalindrome(string str)
    {
    int l = 0, r = str.length() — 1;
    while (l < r) {
    if (str[l] != str[r]) return false;
    l++; r--;
    }
    return true;
    }

These are the basic string operations that are frequently used in competitive programming problems.

Note: I have used ChatGPT mainly for better formatting and clarity while writing this blog. The concepts shared here are what I learned from multiple sources such as Codeforces practice, YouTube, GeeksforGeeks, and regular CP practice.
I would really appreciate any feedback, corrections, or suggestions to improve this blog.
At the end, Happy New Year to all.

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it