Convert all lowercase characters to uppercase whose ASCII value is co-prime with k Last Updated : 08 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer 'k' and a string 'str' consisting of characters from English alphabets. The task is to convert all lower case character to uppercase whose ASCII value is co-prime with k. Examples: Input: str = "geeksforgeeks", k = 4 Output: GEEKSfOrGEEKS 'f' and 'r' are the only characters whose ASCII values aren't co-prime with 4. Input: str = "Ac", k = 2 Output: AC The only lower case character is 'c' and ASCII value of 'c' is 99 which is co-prime with 2. Approach: Iterate over all characters in the given string to check whether the current character is lowercase and if its ASCII value is co-prime with 'k'To check for co-prime, check that if the gcd of the value with k is '1' or not.If the above condition is satisfied then convert that lowercase alphabet to uppercase alphabet.Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // function to modify the string void convert_str(string s, int k) { // length of the string int l = s.length(); for (int i = 0; i < l; i++) { int ascii = (int)s[i]; // check if the character is // lowercase and co-prime with k if (ascii >= 'a' && ascii <= 'z' && __gcd(ascii, k) == 1) { // change the character // to upper-case char c = s[i] - 32; s[i] = c; } } cout << s << "\n"; } // Driver code int main() { string s = "geeksforgeeks"; int k = 4; convert_str(s, k); return 0; } Java // Java implementation of the approach class GFG { // function to modify the string static void convert_str(String str, int k) { // length of the string char[] s = str.toCharArray(); int l = s.length; for (int i = 0; i < l; i++) { int ascii = (int) s[i]; // check if the character is // lowercase and co-prime with k if (ascii >= 'a' && ascii <= 'z' && __gcd(ascii, k) == 1) { // change the character // to upper-case char c = (char) (s[i] - 32); s[i] = c; } } System.out.println(String.valueOf(s)); } static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) { return b; } if (b == 0) { return a; } // base case if (a == b) { return a; } // a is greater if (a > b) { return __gcd(a - b, b); } return __gcd(a, b - a); } // Driver code public static void main(String[] args) { String s = "geeksforgeeks"; int k = 4; convert_str(s, k); } } // This code is contributed by 29AjayKumar Python3 # Python3 implementation of the approach from math import gcd # function to modify the string def convert_str(s, k): modified_string = "" for i in range(0, len(s)): ascii = ord(s[i]) # check if the character is # lowercase and co-prime with k if (ascii >= ord('a') and ascii <= ord('z') and gcd(ascii, k) == 1): # change the character to upper-case modified_string += chr(ascii - 32) else: modified_string += s[i] print(modified_string) # Driver code if __name__ == "__main__": s = "geeksforgeeks" k = 4 convert_str(s, k) # This code is contributed by Rituraj Jain C# // C# implementation of the approach using System; class GFG { // function to modify the string static void convert_str(String str, int k) { // length of the string char[] s = str.ToCharArray(); int l = s.Length; for (int i = 0; i < l; i++) { int ascii = (int) s[i]; // check if the character is // lowercase and co-prime with k if (ascii >= 'a' && ascii <= 'z' && __gcd(ascii, k) == 1) { // change the character // to upper-case char c = (char) (s[i] - 32); s[i] = c; } } Console.WriteLine(String.Join("", s)); } static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) { return b; } if (b == 0) { return a; } // base case if (a == b) { return a; } // a is greater if (a > b) { return __gcd(a - b, b); } return __gcd(a, b - a); } // Driver code public static void Main() { String s = "geeksforgeeks"; int k = 4; convert_str(s, k); } } // This code is contributed by PrinciRaj1992 JavaScript <script> // JavaScript implementation of the approach // function to modify the string function convert_str(s, k) { // length of the string var l = s.length; var newStr = ""; for (var i = 0; i < l; i++) { var ascii = s[i].charCodeAt(0); // check if the character is // lowercase and co-prime with k if ( ascii >= "a".charCodeAt(0) && ascii <= "z".charCodeAt(0) && __gcd(ascii, k) === 1 ) { // change the character // to upper-case var c = String.fromCharCode(s[i].charCodeAt(0) - 32); newStr += c; } else { newStr += s[i]; } } document.write(newStr); } function __gcd(a, b) { // Everything divides 0 if (a === 0) { return b; } if (b === 0) { return a; } // base case if (a === b) { return a; } // a is greater if (a > b) { return __gcd(a - b, b); } return __gcd(a, b - a); } // Driver code var s = "geeksforgeeks"; var k = 4; convert_str(s, k); </script> PHP <?php // PHP implementation of the approach // function to modify the string function convert_str($str, $k) { // length of the string $l = strlen($str); $modified_string = ""; for ($i = 0; $i < $l; $i++) { $ascii = ord($str[$i]); // check if the character is // lowercase and co-prime with k if ($ascii >= ord('a') && $ascii <= ord('z') && __gcd($ascii, $k) == 1) { // change the character to upper-case $modified_string = $modified_string.chr($ascii - 32); } else { $modified_string = $modified_string.$str[$i]; } } echo ($modified_string); } function __gcd($a, $b) { // Everything divides 0 if ($a == 0) { return $b; } if ($b == 0) { return $a; } // base case if ($a == $b) { return $a; } // a is greater if ($a > $b) { return __gcd($a - $b, $b); } return __gcd($a, $b - $a); } // Driver code $s = "geeksforgeeks"; $k = 4; convert_str($s, $k); // This code is contributed by ita_c ?> OutputGEEKSfOrGEEKSComplexity Analysis: Time Complexity: O(L), where L is the length of string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Replace every character of string by character whose ASCII value is K times more than it S souradeep Follow Improve Article Tags : DSA ASCII Similar Reads Count Uppercase, Lowercase, special character and numeric values Given a string, write a program to count the occurrence of Lowercase characters, Uppercase characters, Special characters, and Numeric values. Examples: Input : #GeeKs01fOr@gEEks07 Output : Upper case letters : 5 Lower case letters : 8 Numbers : 4 Special Characters : 2 Input : *GeEkS4GeEkS* Output 5 min read Count pairs of characters in a string whose ASCII value difference is K Given string str of lower case alphabets and a non-negative integer K. The task is to find the number of pairs of characters in the given string whose ASCII value difference is exactly K. Examples: Input: str = "abcdab", K = 0 Output: 2 (a, a) and (b, b) are the only valid pairs.Input: str = "geeksf 7 min read Check if lowercase and uppercase characters are in same order Given string str containing only lower and uppercase alphabets. The task is to check if both lowercase characters and uppercase characters follow the same order respectively. Note: If a character occurs more than once in lowercase then the occurrence of the same character in the uppercase should be 4 min read Replace every character of string by character whose ASCII value is K times more than it Given string str consisting of lowercase letters only and an integer k, the task is to replace every character of the given string with a character whose ASCII value is k times more than it. If the ASCII value exceeds 'z', then start checking from 'a in a cyclic manner. Examples: Input: str = "abc", 9 min read Minimize cost to convert all occurrences of each distinct character to lowercase or uppercase Given a string S consisting of N alphabets and two positive integers L and U, representing the cost of converting any character to lowercase and uppercase respectively, the task is to modify the string S such that every occurrence of the alphabet in the string S is either lowercase or uppercase and 11 min read Check if uppercase characters in a string are used correctly or not Given a string S consisting of uppercase and lowercase letters, the task is to check if uppercase characters are used correctly in the given string or not. Correct usage of uppercase characters are as follows: All characters in the string are in uppercase. For example, "GEEKS".None of the characters 8 min read Like