String generated by typing given string in a keyboard having the button of given character faulty

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.