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";
str.length() or str.size() returns the length of a string str.
str.empty() is used to check whether a string is empty.
if (s.empty())
cout<<"String is empty";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 bothTraversing a String
We can traverse a string like an array or using a range-based loop.
for(char c : str)
cout<<c;To insert a char at the end we use push_back() function (similar to vector)
str.push_back('x');//add at endTo remove last char we use
str.pop_back();String concatenation
string a = "Thank";
string b = "You";
string c = a + " " + b; //Thank youString 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";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.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";To sort a string
sort(str.begin(), str.end());
To convert character case
char c = 'A';
c = tolower(c);//a
char d = 'b';
d = toupper(d);//BIt works on char, not on string. You can use a loop to convert each character of a string.
To store frequency of characters
vector freq(26, 0);
for (char c : s)
freq[c — 'a']++;To reverse a string
reverse(str.begin(),str.end());
stoi() is used to convert string into integer
int num = stoi("143"); //string must contain valid number
to_string() is used to convert integer into a string
string str = to_string(1432);
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.







