Print the string by ignoring alternate occurrences of any character - GeeksforGeeks

20 May, 2021
Given a string of both uppercase and lowercase alphabets, the task is to print the string with alternate occurrences of any character dropped(including space and consider upper and lowercase as same).Examples:  
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
Asked in: Microsoft 
As we have to print characters in an alternate manner, so start traversing the string and perform following two steps:-  
Increment the count of occurrence of current character in a hash table.
Check if the count becomes odd, then print the current character, else not.  
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to print the string in given pattern#include <bits/stdc++.h>using namespace std; // Function to print the stringvoid printStringAlternate(string str){    unordered_map<char, int> 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 codeint main(){    string str = "Geeks for geeks";    string str2 = "It is a long day Dear";     printStringAlternate(str);    printStringAlternate(str2);     return 0;}
// Java program to print the string in given patternimport 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
# Python3 program to print the string# in given pattern # Function to print the stringdef 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 codeif __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# program to print the// string in given patternusing 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.
<?php// PHP program to print the string// in given pattern // Function to print the stringfunction printStringAlternate($str){    $occ = array();         // Convert uppercase to lowercase    $s = strtolower($str);     // Start traversing the string    for ($i = 0; $i < strlen($str); $i++)    {                 $temp = $s[$i];         // Increment occurrence count        $occ[($temp)]++;         // If count is odd        // then print the character        if ($occ[$temp] % 2 != 0)            echo($str[$i]);    }    echo "\n";} // Driver Code$str1 = "Geeks for geeks";$str2 = "It is a long day Dear";printStringAlternate($str1);printStringAlternate($str2); // This code is contributed by Code_Mech.?>
<script>     // JavaScript program to print the    // string in given pattern         // Function to print the string    function printStringAlternate(str)    {        let occ = new Array(122);        occ.fill(0);                   // Convert uppercase to lowercase        let s = str.toLowerCase();              // Start traversing the string        for (let i = 0; i < str.length; i++)        {                           let temp = s[i];               // Increment occurrence count            occ[temp.charCodeAt()]++;               // If count is odd then print            // the character            if (occ[temp.charCodeAt()] % 2 != 0)                document.write(str[i]);        }        document.write("</br>");    }         let str1 = "Geeks for geeks";    let str2 = "It is a long day Dear";    printStringAlternate(str1);    printStringAlternate(str2);     </script>
Output:  
Geks fore
It sa longdy ear
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
rituraj_jain
Code_Mech
suresh07
Microsoft
Strings
Microsoft
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews