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 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 ?> |
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.
Recommended Posts:
- Lower case to upper case - An interesting fact
- Convert camel case string to snake case in Java
- Convert Snake Case string to Camel Case in Java
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Remove all characters other than alphabets from string
- Count of character pairs at same distance as in English alphabets
- Find the length of the longest subsequence with first K alphabets having same frequency
- Longest substring of vowels with no two adjacent alphabets same
- Python | Toggle characters in words having same case
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Count of alphabets having ASCII value less than and greater than k
- Program to generate random alphabets
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Check if words are sorted according to new order of alphabets
- Sub-string that contains all lowercase alphabets after performing the given operation
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
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.

