Given a string str and a character ch, where the button of character ch is not working correctly. After pressing that key, it toggles the Caps Lock instead of that letter. The task is to print the string generated after typing str on such a faulty keyboard.
Note: The string may contain both upper and lower case characters.
Examples:
Input: str = “This keyboard is faulty.”, ch = ‘a’
Output: This keyboRD IS Fulty.
Explanation:
‘a’ is encountered at position 10 (0 based indexing), which turns Caps Lock ON, resulting in capitalizing the further characters till next ‘a’ is encountered at position 18, which turns it OFF.
Therefore the resultant string is “This keyboRD IS Fulty.”.Input: str = “Read the phrase.”, ch = ‘z’
Output: Read the phrase.
Explanation: Since ‘z’ does not occur in the string, the string str is printed correctly.
Approach: Follow the steps below to solve the above problem:
- Initialize a boolean variable CapsLockOn as false that will stores whether the Caps Lock is on or not.
- Traverse the given string character by character and perform the following:
- If any character in the string is the same as ch, then delete the current character and reverse the value of CapsLockOn and continues to iterate the string.
- If CapsLockOn is True, then update the current character to uppercase or vice-versa.
- If CapsLockOn is False, then leave the current character unchanged.
- After complete traversal of the string, print the resultant string.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to return the updated string // after typing on faulty keyboard string faultyKeyboard(string str, char ch) { // Stores the status of Caps Lock bool CapsLockOn = false; int i = 0; // Traverse over the string while (i < str.length()) { // If ch is lower case if (str[i] == ch || str[i] == char(ch + 32)) { // Toggle the caps lock CapsLockOn = !CapsLockOn; // Delete i-th character str.erase(i, 1); continue; } // If ch is upper case else if (str[i] == ch || str[i] == char(ch + 32)) { // Toggle the caps lock CapsLockOn = !CapsLockOn; // Delete i-th character str.erase(i, 1); continue; } // If CapsLockOn is true else if (CapsLockOn) { if (islower(str[i])) { // Convert the lower case // character to upper case str[i] = toupper(str[i]); } else { // Convert the upper case // character to lower case str[i] = tolower(str[i]); } } // Increment for next iteration i++; } // Return the resultant string return str; } // Driver Code int main() { // Given string str string str = "This keyboard is faulty."; char ch = 'a'; // Function Call cout << faultyKeyboard(str, ch); return 0; } |
This keyboRD IS Fulty.
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Modify the string such that every character gets replaced with the next character in the keyboard
- Count sequences of given length having non-negative prefix sums that can be generated by given values
- Check whether given string can be generated after concatenating given strings
- Mth bit in Nth binary string from a sequence generated by the given operations
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Minimum moves required to type a word in QWERTY based keyboard
- Find the kth element in the series generated by the given N ranges
- Find the element in the matrix generated by given rules
- Find the sum of elements of the Matrix generated by the given rules
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Maximize sum of pairwise products generated from the given Arrays
- Nth term of a recurrence relation generated by two given arrays
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Bitwise XOR of a submatrix of a matrix generated from a given array
- Mean of array generated by products of all pairs of the given array
- Complete the sequence generated by a polynomial
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Find the Kth smallest element in the sorted generated array
- Find determinant of matrix generated by array rotation
- Find the value at kth position in the generated array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

