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: GeFoGeInput: 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 chracters// from a string that are in same case#include <bits/stdc++.h>using namespace std; // Function to return the modified stringstring 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 codeint 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 caseusing System; class GFG{ // Function to return the modified stringstatic 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 codepublic 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 stringfunction 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?> |
Output:
GeFoGe
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.


