Cognizant GenC Elevate Sample Coding Question 9

Question 9

In this article, we will discuss about Coding Question with their solution in C++ and java. In this question , we have to find the number of words that remain same after rotating the characters of the word left to right N times.

Question 9

Question 9

Vira writes an apology letter to anu. However, before Anu can read it, Vira’s enemy Rohan takes it and rotates the characters of each word left to right N times. Find the number of words that remain the same even after this shifting of letters.

Input Specification:

  • input1: String of words
  • input2: N, number of times rotation happens

Output Specification:

  • Your function should return the number of correct words.

Example 1:

input1: llohe ereth
input2: 2

Output : 2

Example 2:

input1: Sorry
input2: 2

Output : 0

#include<bits/stdc++.h>
using namespace std;

// In-place rotates s towards left by d
void leftrotate(string &s, int d)
{
reverse(s.begin(), s.begin()+d);
reverse(s.begin()+d, s.end());
reverse(s.begin(), s.end());
}

// Driver code
int main()
{
string str1;
cin>>str1;
Int n;
cin>>n;
leftrotate(str1, n);
cout << str1 << endl;

return 0;
}