w3resource

C++ Exercises: Check if a given string starts with 'C#' or not

C++ Basic Algorithm: Exercise-12 with Solution

Write a C++ program to check if a given string starts with 'C#' or not.

Sample Solution:

C++ Code :

#include <iostream>

using namespace std;

 bool test(string str)
        {
            return (str.length() < 3 && str=="C#") || (str.substr(0,2)=="C#" && str[2] == ' ');
        }
        
int main() 
 {
  cout << test("C# Sharp") << endl;  
  cout << test("C#") << endl;  
  cout << test("C++") << endl;  
  return 0;    
}

Sample Output:

1
1
0

Pictorial Presentation:

C++ Basic Algorithm Exercises: Check if a given string starts with 'C#' or not.

Flowchart:

Flowchart: Check if a given string starts with 'C#' or not.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.
Next: Write a C++ program to check if one given temperatures is less than 0 and the other is greater than 100.

What is the difficulty level of this exercise?