Remove consecutive alphabets which are in same case

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to remove the consecutive chracters
// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

GeFoGe


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.