Remove consecutive alphabets which are in same case
Last Updated :
13 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
string removeChars(string s)
{
string modifiedStr = "" ;
modifiedStr += s[0];
for ( int i = 1; i < s.length(); i++) {
if ( isupper (s[i]) && islower (s[i - 1])
|| islower (s[i]) && isupper (s[i - 1]))
modifiedStr += s[i];
}
return modifiedStr;
}
int main()
{
string s = "GeeksForGeeks" ;
cout << removeChars(s);
return 0;
}
|
Java
class GFG
{
static String removeChars(String s)
{
String modifiedStr = "" ;
modifiedStr += s.charAt( 0 );
for ( int i = 1 ; i < s.length(); i++)
{
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;
}
public static void main(String []args)
{
String s = "GeeksForGeeks" ;
System.out.println(removeChars(s));
}
}
|
Python3
def removeChars(s) :
modifiedStr = ""
modifiedStr + = s[ 0 ]
for i in range ( 1 , len (s)) :
if (s[i].isupper() and s[i - 1 ].islower() or
s[i].islower() and s[i - 1 ].isupper()) :
modifiedStr + = s[i]
return modifiedStr
if __name__ = = "__main__" :
s = "GeeksForGeeks"
print (removeChars(s))
|
C#
using System;
class GFG
{
static string removeChars( string s)
{
string modifiedStr = "" ;
modifiedStr += s[0];
for ( int i = 1; i < s.Length; i++)
{
if ( char .IsUpper(s[i]) && char .IsLower(s[i - 1]) ||
char .IsLower(s[i]) && char .IsUpper(s[i - 1]))
modifiedStr += s[i];
}
return modifiedStr;
}
public static void Main()
{
string s = "GeeksForGeeks" ;
Console.Write(removeChars(s));
}
}
|
PHP
<?php
function removeChars( $s )
{
$modifiedStr = "" ;
$modifiedStr = $modifiedStr . $s [0];
for ( $i = 1; $i < strlen ( $s ); $i ++)
{
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 ;
}
$s = "GeeksForGeeks" ;
echo removeChars( $s );
?>
|
Javascript
<script>
function removeChars(s)
{
let modifiedStr = "" ;
modifiedStr += s[0];
for (let i = 1; i < s.length; i++)
{
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;
}
let s = "GeeksForGeeks" ;
document.write(removeChars(s));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...