📌  相关文章
📜  删除相同大小写的连续字母

📅  最后修改于: 2022-05-13 01:57:06.320000             🧑  作者: Mango

删除相同大小写的连续字母

给定一个字符串str ,任务是从字符串中删除相同大小写(大写或小写)的连续字符并打印结果字符串。

例子:

方法:

  • 按原样打印第一个字符。
  • 从第二个字符开始遍历字符串中的所有其他字符。
  • 比较当前和以前的字符:
    • 如果当前字符和前一个字符大小写相同,则跳过。
    • 否则打印当前字符。

下面是上述方法的实现:

C++
// C++ program to remove the consecutive characters
// from a string that are in same case
#include 
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


Javascript


输出:
GeFoGe