logo codesdope

Practice questions related to string and std::string.


Level 1
1.
Write a program to print a string entered by user.
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string x;
  cout << "Enter the string" << endl;
  cin >> x;
  cout << x << endl;
  return 0;
}
                                   

2.
Write a program to print every character of a string entered by user in a new line using loop.
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string x;
  cout << "Enter the string" << endl;
  cin >> x;
  for(int i=0; i<x.length(); i++)
    cout << x[i] << endl;
  return 0;
}
                                   

3.
Write a program to input and display the sentence I love candies.

4.
Write a program to find the length of the string "refrigerator".

5.
Create an array of characters and then print the address of each of the elements of the array. Take difference of two consecutive addresses and compare this with array of integers.

6.
Write a program to enter a string s1 and copy it to another string s2.
Hint
You can directly copy std::string with =. You can use strcpy for C-strings or you can make your own function using for loop and copy each character.

7.
Write a program to compare if the two strings entered by user are equal or not without using predefined String functions.
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string x,y;

  cout << "Enter first string" << endl;
  cin >> x;
  cout << "Enter secont string" << endl;
  cin >> y;

  bool equal = true;

  //length must be equal
  if(x.length()!=y.length())
  {
    equal = false;
  }
  else
  {
    for(int i=0;i<x.length();i++)
    {
      if(x[i]!=y[i])
      {
        equal = 0;
        break;
      }
    }
  }

  cout << equal << endl;

  return 0;
}
                                   

8.
Write a program to check if the letter 'e' is present in the word 'Umbrella'.

9.
Write a program to check if the word 'orange' is present in the "This is orange juice".
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s1 = "orange";
  string s2 = "This is orange juice";
  string s3;
  bool present = false;

  for(int i=0; i<(s2.length()-s1.length())+1;i++)
  {
    s3 = s2.substr(i,s1.length());
    if(s1.compare(s3) == 0)
    {
      present = true;
      break;
    }
  }

  cout << present << endl;

  return 0;
}

                                   

10.
Write a program to find the first and the last occurence of the letter 'o' and character ',' in "Hello, World".
Hint
For the last occurence, iterate from end.

11.
Write the string after the first occurrence of ',' and the string after the last occurrence of ',' in the string "Hello, Good, Morning".
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s1 = "Hello, Good, Morning";
  int first_ocr, last_ocr;
  for(int i=0;i<s1.length(); i++)
  {
    if(s1[i] == ',')
    {
      first_ocr = i;
      break;
    }
  }
  for(int i=s1.length()-1;i>=0;i--)
  {
    if(s1[i] == ',')
    {
      last_ocr = i;
      break;
    }
  }

  cout << s1.substr(first_ocr+1) << endl;
  cout << s1.substr(last_ocr+1) << endl;

  return 0;
}

                                   

12.
Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
Hint
1. Start from the end.
2. Check for the last word. There will be a space before the first character of the last word or the first character will also be the first character of the string if the string contains only one word.
3. Copy this last word in a new string.
4. Repeat step 2 for the middle word but this time only insert the first character and a dot(.) instead of the whole word in the new string.
5. Repeate till the loop ends by encountering the first character of the string.

13.
Write a program to find the number of vowels, consonents, digits and white space characters in a string.

14.
Write a program to delete all consonents from the string "Hello, have a good day".

15.
Input a string of alphabets. Find out the number of occurrence of all alphabets in that string. Find out the alphabet with maximum occurrence.

Level 2
1.
Write a program to reverse a string with and without using any predefined function.
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s1 = "ABCD";
  string s2;
  s2.resize(s1.length());
  for(int i=0; i<s1.length(); i++)
  {
    s2[s1.length()-i-1] = s1[i];
  }
  cout << s2 << endl;
  return 0;
}

                                   

2.
Write a program to find the length of a string without using predefined functions.

3.
Check the occurrence of the letter 'e' and the word 'is' in the sentence "This is umbrella" without using predefined functions.

4.
Write a program to find the number of vowels, consonents, digits and white space characters in a string

5.
Write a program to concatenate two strings "Hello" and "World".

6.
Write a program to find out the largest and smallest word in the string "This is an umbrella".
Hint
You can assume that there is a single space between two words, except the last word.

7.
Write a program to check if a given string is a Palindrome.
A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.
Answer
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s1 = "aba";
  bool pali = true;
  for(int i=0; i<s1.length(); i++)
  {
    if(s1[i] != s1[s1.length()-i-1])
    {
      pali = false;
      break;
    }
  }
  cout << pali << endl;
  return 0;
}

                                   

8.
Write down the names of 10 of your friends in an array and then sort those in alphabetically ascending order.
Hint
You can use any sorting algorithm (like one in the practice section of the chapter array) and compare using strcmp function.

9.
Write a program to delete all the consonents from the string "Hello, have a good day".

10.
Write a program to delete the word "the" in the sentence "This is the lion in the cage".

11.
Write a program to check if the two strings entered by user are anagrams or not. Two words are said to be anagrams if the letters of one word can be rearranged to form the other word. For example, jaxa and ajax are anagrams of each other.
Answer
A better approach would be to map every character of english alphabets with elements of an array having 26 (or 26*2 considering cases) elements representing each alphabet. Try it yourself.
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s1 = "ajax";
  string s2 = "jaxa";
  bool anagram = true;

  //only if both strings have same length
  if(s1.length()!=s2.length())
    anagram = false;
  else
  {
    //matching every element of s2 with every element of s1
    for(int i=0; i<s1.length(); i++)
    {
      bool found = false;
      for(int j=0; j<s2.length(); j++)
      {
        if(s1[i] == s2 [j])
        {
          //if equal then equating it to empty char
          //so doesn't match again
          s2[j] = ' ';
          found = true;
          break;
        }
      }
      //if not found then it is not an anagram
      if(!found)
      {
        anagram = false;
        break;
      }
    }
  }

  cout << anagram << endl;

  return 0;
}

                                   

12.
Input a string which contains some palindrome substrings. Find out the position of palindrome substrings if exist and replace it by *. (For example if input string is “bob has a radar plane” then it should convert in “*** has a ***** plane”.

13.
Write a program to replace a given substring in a sentence with another string. For example, in the sentence, ” A batman with bat” if we replace ”bat” with ”snow”, the new sentence should be printed as ”A snowman with snow”.

14.
Write a program to reverse individual words in a string, where each word may be delimited by a dot, comma, space or tab, like www.google.com should become www.elgoog.moc.

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.