原文:

给定一个由大写字母和小写字母组成的字符串,任务是打印字符串,并替换掉任何字符(包括空格,并认为大写和小写相同)。 例:

input : it is a long day dear.
output : it sa longdy ear.
print first i and then ignore next i.
similarly print first space then 
ignore next space.
input : geeks for geeks
output : geks fore

问于:t2】

由于我们必须以另一种方式打印字符,因此开始遍历字符串并执行以下两个步骤:-

  • 增加哈希表中当前字符的出现次数。
  • 检查计数是否为奇数,然后打印当前字符,否则不打印。

c

// c   program to print the string in given pattern
#include 
using namespace std;
// function to print the string
void printstringalternate(string str)
{
    unordered_map occ;
    // start traversing the string
    for (int i = 0; i < str.length(); i  ) {
        // convert uppercase to lowercase
        char temp = tolower(str[i]);
        // increment occurrence count
        occ[temp]  ;
        // if count is odd then print the character
        if (occ[temp] & 1)
            cout << str[i];
    }
    cout << endl;
}
// drivers code
int main()
{
    string str = "geeks for geeks";
    string str2 = "it is a long day dear";
    printstringalternate(str);
    printstringalternate(str2);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the string in given pattern
import java.io.*;
class gfg
{
    // function to print the string
    static void printstringalternate(string str)
    {
        int[] occ = new int[122];
        // convert uppercase to lowercase
        string s = str.tolowercase();
        // start traversing the string
        for (int i = 0; i < str.length(); i  )
        {
            char temp = s.charat(i);
            // increment occurrence count
            occ[temp]  ;
            // if count is odd then print the character
            if (occ[temp]%2 != 0)
                system.out.print(str.charat(i));
        }
        system.out.println();
    }
    // driver program
    public static void main (string[] args)
    {
        string str1 = "geeks for geeks";
        string str2 = "it is a long day dear";
        printstringalternate(str1);
        printstringalternate(str2);
    }
}
// contributed by pramod kumar

python 3

# python3 program to print the string
# in given pattern
# function to print the string
def printstringalternate(string):
    occ = {}
    # start traversing the string
    for i in range(0, len(string)):
        # convert uppercase to lowercase
        temp = string[i].lower()
        # increment occurrence count
        occ[temp] = occ.get(temp, 0)   1
        # if count is odd then print the character
        if occ[temp] & 1:
            print(string[i], end = "")
    print()
# driver code
if __name__ == "__main__":
    string = "geeks for geeks"
    string2 = "it is a long day dear"
    printstringalternate(string)
    printstringalternate(string2)
# this code is contributed by rituraj jain

c

// c# program to print the
// string in given pattern
using system;
public class gfg {
    // function to print the string
    static void printstringalternate(string str)
    {
        int[] occ = new int[122];
        // convert uppercase to lowercase
        string s = str.tolower();
        // start traversing the string
        for (int i = 0; i < str.length; i  )
        {
            char temp = s[i];
            // increment occurrence count
            occ[temp]  ;
            // if count is odd then print
            // the character
            if (occ[temp] % 2 != 0)
                console.write(str[i]);
        }
        console.writeline();
    }
    // driver code
    public static void main ()
    {
        string str1 = "geeks for geeks";
        string str2 = "it is a long day dear";
        printstringalternate(str1);
        printstringalternate(str2);
    }
}
// this code is contributed by sam007.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

geks fore
it sa longdy ear

本文由 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。