Convert all lowercase characters to uppercase whose ASCII value is co-prime with k in C++


In this tutorial, we will be discussing a program to convert all lowercase characters to uppercase whose ASCII value is co-prime with k.

For this we will be provided with a string and an integer value k. Our task is to traverse through the given string and change to uppercase all those characters whose ASCII value is co-prime with the given integer k.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//modifying the given string
void convert_string(string s, int k){
   int l = s.length();
   for (int i = 0; i < l; i++) {
      int ascii = (int)s[i];
      //checking if the value is coprime with k
      if (ascii >= 'a' && ascii <= 'z'&& __gcd(ascii, k) == 1) {
         char c = s[i] - 32;
         s[i] = c;
      }
   }
   cout << s << "\n";
}
int main(){
   string s = "tutorialspoint";
   int k = 3;
   convert_string(s, k);
   return 0;
}

Output

TuToriAlSPoiNT

Updated on: 06-Jan-2020

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements