
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Lowercase Characters to Uppercase Based on Co-Prime ASCII Value 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
#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
Advertisements