Skip to content
Related Articles

Related Articles

Remove consecutive alphabets which are in same case

Improve Article
Save Article
  • Last Updated : 13 Sep, 2022
Improve Article
Save Article

Given a string str, the task is to remove consecutive characters from the string that are in same case (Uppercase or Lowercase) and print the resultant string.

Examples:

Input: str = “GeeksForGeeks” 
Output: GeFoGe

Input: str = “abcDEFghi” 
Output: aDg 

Approach: 

  • Print the first character as it is.
  • Traverse through all the other characters in the string starting from second character.
  • Compare the current and the previous characters: 
    • If the current and the previous characters are in the same case then skip.
    • Else print the current character.

Below is the implementation of the above approach: 

C++




// C++ program to remove the consecutive characters
// from a string that are in same case
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the modified string
string removeChars(string s)
{
    string modifiedStr = "";
    modifiedStr += s[0];
 
    // Traverse through the remaining
    // characters in the string
    for (int i = 1; i < s.length(); i++) {
 
        // If the current and the previous
        // characters are not in the same
        // case then take the character
        if (isupper(s[i]) && islower(s[i - 1])
            || islower(s[i]) && isupper(s[i - 1]))
            modifiedStr += s[i];
    }
 
    return modifiedStr;
}
 
// Driver code
int main()
{
    string s = "GeeksForGeeks";
    cout << removeChars(s);
    return 0;
}

Java




// Java program to remove the consecutive characters
// from a string that are in same case
 
class GFG
{
     
    // Function to return the modified string
    static String removeChars(String s)
    {
        String modifiedStr = "";
        modifiedStr += s.charAt(0);
     
        // Traverse through the remaining
        // characters in the string
        for (int i = 1; i < s.length(); i++)
        {
     
            // If the current and the previous
            // characters are not in the same
            // case then take the character
            if (Character.isUpperCase(s.charAt(i)) && Character.isLowerCase(s.charAt(i - 1)) ||
                Character.isLowerCase(s.charAt(i)) && Character.isUpperCase(s.charAt(i - 1)))
                modifiedStr += s.charAt(i);
        }
     
        return modifiedStr;
    }
     
    // Driver code
    public static void main(String []args)
    {
        String s = "GeeksForGeeks";
        System.out.println(removeChars(s));
    }
}
     
// This code is contributed
// by ihritik

Python3




# Python3 program to remove the consecutive
# characters from a string that are in same case
 
# Function to return the modified string
def removeChars(s) :
 
    modifiedStr = ""
    modifiedStr += s[0]
 
    # Traverse through the remaining
    # characters in the string
    for i in range(1, len(s)) :
 
        # If the current and the previous
        # characters are not in the same
        # case then take the character
        if (s[i].isupper() and s[i - 1].islower() or
            s[i].islower() and s[i - 1].isupper()) :
                 
            modifiedStr += s[i]
     
    return modifiedStr
 
# Driver code
if __name__ == "__main__" :
 
    s = "GeeksForGeeks"
    print(removeChars(s))
 
# This code is contributed by Ryuga

C#




// C# program to remove the consecutive characters
// from a string that are in same case
using System;
 
class GFG
{
     
// Function to return the modified string
static string removeChars(string s)
{
    string modifiedStr = "";
    modifiedStr += s[0];
 
    // Traverse through the remaining
    // characters in the string
    for (int i = 1; i < s.Length; i++)
    {
 
        // If the current and the previous
        // characters are not in the same
        // case then take the character
        if (char.IsUpper(s[i]) && char.IsLower(s[i - 1]) ||
            char.IsLower(s[i]) && char.IsUpper(s[i - 1]))
            modifiedStr += s[i];
    }
 
    return modifiedStr;
}
 
// Driver code
public static void Main()
{
    string s = "GeeksForGeeks";
    Console.Write(removeChars(s));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP




<?php
// PHP program to remove the consecutive characters
// from a string that are in same case
 
// Function to return the modified string
function removeChars($s)
{
    $modifiedStr = "";
    $modifiedStr = $modifiedStr . $s[0];
 
    // Traverse through the remaining
    // characters in the string
    for ($i = 1; $i < strlen($s); $i++)
    {
 
        // If the current and the previous
        // characters are not in the same
        // case then take the character
        if (ctype_upper($s[$i]) && ctype_lower($s[$i - 1]) ||
            ctype_lower($s[$i]) && ctype_upper($s[$i - 1]))
            $modifiedStr = $modifiedStr . $s[$i];
    }
 
    return $modifiedStr;
}
 
// Driver code
$s = "GeeksForGeeks";
echo removeChars($s);
 
// This code is contributed by ita_c
?>

Javascript




<script>
 
// JavaScript program to remove
// the consecutive characters
// from a string that are in same case
     
    // Function to return the modified string
    function removeChars(s)
    {
        let modifiedStr = "";
        modifiedStr += s[0];
       
        // Traverse through the remaining
        // characters in the string
        for (let i = 1; i < s.length; i++)
        {
       
            // If the current and the previous
            // characters are not in the same
            // case then take the character
            if (s[i] == (s[i]).toUpperCase() &&
            (s[i - 1])==(s[i - 1]).toLowerCase() ||
                s[i]==s[i].toLowerCase() &&
                (s[i - 1])==(s[i - 1]).toUpperCase())
                modifiedStr += s[i];
        }
       
        return modifiedStr;
    }
     
    // Driver code
    let s = "GeeksForGeeks";
    document.write(removeChars(s));
     
 
 
// This code is contributed by avanitrachhadiya2155
 
</script>

Output

GeFoGe

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!