w3resource

C++ Exercises: Check if a string 'yt' appears at index 1 in a given string

C++ Basic Algorithm: Exercise-17 with Solution

Write a C++ program to check if a string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

string test(string str)
        {
            return str.substr(1, 2)=="yt" ? str.erase(1, 2) : str;
        }
        
int main() 
 {
  cout << test("Python") << endl;  
  cout << test("ytade") << endl;  
  cout << test("jsues") << endl;  
  return 0;    
}

Sample Output:

Phon
ytade
jsues

Pictorial Presentation:

C++ Basic Algorithm Exercises: Check if a string 'yt' appears at index 1 in a given string.

Flowchart:

Flowchart: Check if a string 'yt' appears at index 1 in a given string.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the said range otherwise false.
Next: Write a C++ program to check the largest number among three given integers.

What is the difficulty level of this exercise?