Q:

Write a C++ program to check whether the first two characters and last two characters of a given string are same

0

Write a C++ program to check whether the first two characters and last two characters of a given string are same

Sample Output:

1
0
1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;

bool test(string s1) 
        {
           return s1.substr(0, 2) == s1.substr(s1.length() - 2);
        }
        
int main() 
 {
  cout << test("abab") << endl;  
  cout << test("abcdef") << endl; 
  cout << test("xyzsderxy") << endl;  
  return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now