C++ Strings Practice Questions with Solutions

Strings are one of the most commonly used data types in C++ programming. A string is a sequence of characters used to store and manipulate text such as names, sentences, passwords, email addresses, and more.

In modern C++, strings are handled using the string class from the <string> header file. Unlike character arrays, the string class provides many built-in functions that make string manipulation easier and safer.

Strings are widely used in:

  • User Input Processing
  • Text Processing
  • Search Algorithms
  • Password Validation
  • File Handling
  • Competitive Programming
  • Software Development
  • Web Development

Some common string operations include:

  • Reading a string
  • Displaying a string
  • Finding the length of a string
  • Concatenating strings
  • Comparing strings
  • Reversing strings
  • Checking for palindromes
  • Counting vowels and consonants

In this chapter, you’ll solve practical string-based problems that improve your understanding of C++ string handling.

Each question includes:

  • Problem Statement
  • Complete C++ Solution
  • Sample Input
  • Sample Output
  • Explanation
  • Concepts Covered

Let’s begin with the first five C++ string practice questions. C++ Strings practice questions with solutions help to understand the concepts..


1. C++ Program to Read and Display a String

Problem Statement

Write a C++ program to read a string from the user and display it.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;

    cout << "Enter your name: ";
    getline(cin, name);

    cout << "You entered: "
         << name;

    return 0;
}

Sample Input

Enter your name: Rishabh Kumar

Sample Output

You entered: Rishabh Kumar

Explanation

The getline() function reads the complete line, including spaces.

Concepts Covered

  • string Class
  • getline()
  • User Input
  • String Output

2. C++ Program to Find the Length of a String

Problem Statement

Write a C++ program to find the length of a string.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text;

    cout << "Enter a string: ";
    getline(cin, text);

    cout << "Length = "
         << text.length();

    return 0;
}

Sample Input

Enter a string: Programming

Sample Output

Length = 11

Explanation

The length() function returns the total number of characters present in the string.

Concepts Covered

  • String Functions
  • length()
  • String Operations

3. C++ Program to Concatenate Two Strings

Problem Statement

Write a C++ program to join two strings into one string.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string firstName;
    string lastName;

    cout << "Enter first name: ";
    getline(cin, firstName);

    cout << "Enter last name: ";
    getline(cin, lastName);

    string fullName = firstName + " " + lastName;

    cout << "Full Name = "
         << fullName;

    return 0;
}

Sample Input

Enter first name: Rishabh
Enter last name: Kumar

Sample Output

Full Name = Rishabh Kumar

Explanation

The + operator combines multiple strings into a single string.

Concepts Covered

  • String Concatenation
    • Operator
  • String Variables

4. C++ Program to Compare Two Strings

Problem Statement

Write a C++ program to compare two strings.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first;
    string second;

    cout << "Enter first string: ";
    getline(cin, first);

    cout << "Enter second string: ";
    getline(cin, second);

    if (first == second)
    {
        cout << "Strings are Equal";
    }
    else
    {
        cout << "Strings are Not Equal";
    }

    return 0;
}

Sample Input

Enter first string: Hello
Enter second string: Hello

Sample Output

Strings are Equal

Explanation

The equality operator (==) compares both strings character by character.

Concepts Covered

  • String Comparison
  • Equality Operator
  • Conditional Statements

5. C++ Program to Reverse a String

Problem Statement

Write a C++ program to reverse a string.

C++ Solution

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string text;

    cout << "Enter a string: ";
    getline(cin, text);

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

    cout << "Reversed String = "
         << text;

    return 0;
}

Sample Input

Enter a string: Computer

Sample Output

Reversed String = retupmoC

Explanation

The reverse() function from the <algorithm> library reverses the characters of the string.

Concepts Covered

  • reverse()
  • String Iterators
  • Algorithm Library
  • String Manipulation

6. C++ Program to Count Vowels and Consonants in a String

Problem Statement

Write a C++ program to count the number of vowels and consonants in a given string.

C++ Solution

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string text;
    int vowels = 0;
    int consonants = 0;

    cout << "Enter a string: ";
    getline(cin, text);

    for (char ch : text)
    {
        ch = tolower(ch);

        if (isalpha(ch))
        {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                vowels++;
            else
                consonants++;
        }
    }

    cout << "Vowels = " << vowels << endl;
    cout << "Consonants = " << consonants;

    return 0;
}

Sample Input

Enter a string: Programming

Sample Output

Vowels = 3
Consonants = 8

Explanation

The program checks each alphabetic character and determines whether it is a vowel or a consonant.

Concepts Covered

  • String Traversal
  • Character Classification
  • tolower()
  • isalpha()

7. C++ Program to Convert a String to Uppercase

Problem Statement

Write a C++ program to convert all lowercase letters in a string to uppercase.

C++ Solution

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string text;

    cout << "Enter a string: ";
    getline(cin, text);

    for (char &ch : text)
    {
        ch = toupper(ch);
    }

    cout << "Uppercase String = "
         << text;

    return 0;
}

Sample Input

Enter a string: Hello World

Sample Output

Uppercase String = HELLO WORLD

Explanation

The toupper() function converts every alphabetic character to uppercase.

Concepts Covered

  • Character Conversion
  • toupper()
  • Range-Based for Loop

8. C++ Program to Convert a String to Lowercase

Problem Statement

Write a C++ program to convert all uppercase letters in a string to lowercase.

C++ Solution

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string text;

    cout << "Enter a string: ";
    getline(cin, text);

    for (char &ch : text)
    {
        ch = tolower(ch);
    }

    cout << "Lowercase String = "
         << text;

    return 0;
}

Sample Input

Enter a string: CPLUSPLUS

Sample Output

Lowercase String = cplusplus

Explanation

The tolower() function converts every uppercase letter into its lowercase equivalent.

Concepts Covered

  • Character Conversion
  • tolower()
  • String Manipulation

9. C++ Program to Check Whether a String is a Palindrome

Problem Statement

Write a C++ program to determine whether a string is a palindrome.

A palindrome string reads the same from left to right and right to left.

C++ Solution

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string original;
    string reversed;

    cout << "Enter a string: ";
    getline(cin, original);

    reversed = original;

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

    if (original == reversed)
        cout << "Palindrome String";
    else
        cout << "Not a Palindrome String";

    return 0;
}

Sample Input

Enter a string: madam

Sample Output

Palindrome String

Explanation

The program creates a copy of the original string, reverses it, and compares both strings.

Concepts Covered

  • String Comparison
  • reverse()
  • Palindrome Logic

10. C++ Program to Count the Number of Words in a Sentence

Problem Statement

Write a C++ program to count the total number of words in a sentence.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string sentence;
    int words = 1;

    cout << "Enter a sentence: ";
    getline(cin, sentence);

    for (char ch : sentence)
    {
        if (ch == ' ')
            words++;
    }

    cout << "Total Words = "
         << words;

    return 0;
}

Sample Input

Enter a sentence: C Plus Plus Programming Language

Sample Output

Total Words = 5

Explanation

The program counts the spaces in the sentence. Since words are separated by spaces, the total number of words is equal to the number of spaces plus one.

Concepts Covered

  • String Traversal
  • Word Counting
  • Character Comparison

11. C++ Program to Count the Frequency of a Character in a String

Problem Statement

Write a C++ program to count how many times a particular character appears in a string.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text;
    char character;
    int count = 0;

    cout << "Enter a string: ";
    getline(cin, text);

    cout << "Enter character to search: ";
    cin >> character;

    for (char ch : text)
    {
        if (ch == character)
            count++;
    }

    cout << "Frequency = "
         << count;

    return 0;
}

Sample Input

Enter a string: programming
Enter character to search: g

Sample Output

Frequency = 2

Explanation

The program traverses the string and increments the counter whenever the target character is found.

Concepts Covered

  • String Traversal
  • Character Comparison
  • Counter Variable

12. C++ Program to Remove All Spaces from a String

Problem Statement

Write a C++ program to remove all spaces from a string.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text;
    string result = "";

    cout << "Enter a string: ";
    getline(cin, text);

    for (char ch : text)
    {
        if (ch != ' ')
            result += ch;
    }

    cout << "String Without Spaces = "
         << result;

    return 0;
}

Sample Input

Enter a string: C Plus Plus Programming

Sample Output

String Without Spaces = CPlusPlusProgramming

Explanation

The program copies only non-space characters into a new string.

Concepts Covered

  • String Traversal
  • String Concatenation
  • Character Comparison

13. C++ Program to Count Digits, Alphabets, and Special Characters

Problem Statement

Write a C++ program to count alphabets, digits, and special characters present in a string.

C++ Solution

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string text;
    int alphabets = 0;
    int digits = 0;
    int special = 0;

    cout << "Enter a string: ";
    getline(cin, text);

    for (char ch : text)
    {
        if (isalpha(ch))
            alphabets++;
        else if (isdigit(ch))
            digits++;
        else if (ch != ' ')
            special++;
    }

    cout << "Alphabets = " << alphabets << endl;
    cout << "Digits = " << digits << endl;
    cout << "Special Characters = " << special;

    return 0;
}

Sample Input

Enter a string: C++2026!

Sample Output

Alphabets = 1
Digits = 4
Special Characters = 3

Explanation

The program uses built-in character functions to classify every character.

Concepts Covered

  • isalpha()
  • isdigit()
  • Character Classification
  • String Traversal

14. C++ Program to Check Whether Two Strings are Anagrams

Problem Statement

Write a C++ program to determine whether two strings are anagrams.

Two strings are anagrams if they contain the same characters in a different order.

Example:

listen
silent

C++ Solution

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string first, second;

    cout << "Enter first string: ";
    cin >> first;

    cout << "Enter second string: ";
    cin >> second;

    sort(first.begin(), first.end());
    sort(second.begin(), second.end());

    if (first == second)
        cout << "Anagram";
    else
        cout << "Not an Anagram";

    return 0;
}

Sample Input

Enter first string: listen
Enter second string: silent

Sample Output

Anagram

Explanation

Both strings are sorted alphabetically. If the sorted strings are identical, they are anagrams.

Concepts Covered

  • sort()
  • String Comparison
  • Algorithm Library

15. C++ Program to Find the Longest Word in a Sentence

Problem Statement

Write a C++ program to find the longest word in a sentence.

C++ Solution

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string sentence;
    string word;
    string longest = "";

    cout << "Enter a sentence: ";
    getline(cin, sentence);

    stringstream ss(sentence);

    while (ss >> word)
    {
        if (word.length() > longest.length())
        {
            longest = word;
        }
    }

    cout << "Longest Word = "
         << longest;

    return 0;
}

Sample Input

Enter a sentence: Cplusplus programming language tutorial

Sample Output

Longest Word = Cplusplus

Explanation

The program splits the sentence into individual words using stringstream and keeps track of the longest word.

Concepts Covered

  • stringstream
  • String Parsing
  • length()
  • String Comparison

Chapter Summary

In this chapter, you learned how to work with strings in C++. You practiced reading strings, finding string length, concatenating and comparing strings, reversing text, counting vowels and consonants, converting case, checking palindrome strings, counting words, removing spaces, finding character frequency, detecting anagrams, and identifying the longest word in a sentence. These string operations are essential for solving text-processing problems in interviews, competitive programming, and real-world software applications.


Key Takeaways

  • The string class simplifies text handling in C++.
  • getline() reads complete lines including spaces.
  • Built-in functions like length(), toupper(), and tolower() simplify string operations.
  • reverse() and sort() from the <algorithm> library are useful for many string problems.
  • stringstream is useful for splitting sentences into words.
  • Character functions like isalpha() and isdigit() classify characters efficiently.
  • Strings are heavily used in competitive programming and interviews.
  • Understanding string manipulation is essential before learning advanced algorithms like pattern matching and dynamic programming.
  • Many real-world applications rely on efficient string processing.
  • Mastering strings improves problem-solving skills for coding interviews.

Frequently Asked Questions (FAQs)

1. What is a string in C++?

A string is a sequence of characters represented using the string class from the <string> header.


2. What is the difference between cin and getline()?

  • cin reads input until the first space.
  • getline() reads the entire line, including spaces.

3. How do you find the length of a string?

Use the built-in length() or size() function.


4. How can you reverse a string in C++?

You can use the reverse() function from the <algorithm> library.


5. What is an anagram?

Two strings are anagrams if they contain the same characters with the same frequency but in a different order.


6. Which library is required for the string class?

The <string> header file.


7. What is stringstream used for?

stringstream is used to split or parse text into individual words or values.


8. Why are strings important in programming?

Strings are used for handling names, messages, passwords, file content, user input, search operations, and many real-world text-processing tasks, making them one of the most essential data types in C++.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top