Queries for characters in a repeated string in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find queries for characters in a repeated string.

For this we will be provided with a string of length X and Q queries. Our task is to repeat the given string X times and run Q queries to find the values at index i and j are equal or not.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//checking if i and j values
//are equal or not
void query(char s[], int i, int j) {
   int n = strlen(s);
   i %= n;
   j %= n;
   (s[i]==s[j])? (cout << "Yes" << endl): (cout << "No" << endl);
}
int main() {
   char X[] = "geeksforgeeks";
   query(X, 0, 8);
   query(X, 8, 13);
   query(X, 6, 15);
   return 0;
}

Output

Yes
Yes
No
raja
Published on 27-Jul-2020 12:13:03
Advertisements