Remove odd indexed characters from a given string

Given string str of size N, the task is to remove the characters present at odd indices (0-based indexing) of a given string.

Examples : 

Input: str = “abcdef”
Output: ace
Explanation:
The characters ‘b’, ‘d’ and ‘f’ are present at odd indices, i.e. 1, 3 and 5 respectively. Therefore, they are removed from the string.

Input: str = “geeks”
Output: ges

Approach: Follow the steps below to solve the problem:



  • Initialize an empty string, say new_string, to store the result.
  • Traverse the given string and for every index, check if it is even or not.
  • If found to be true, append the characters at those indices to the string new_string.
  • Finally, after complete traversal of the entire string, return the new_string.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to remove the odd
// indexed characters from a given string
string removeOddIndexCharacters(string s)
{
  
    // Stores the resultant string
    string new_string = "";
  
    for (int i = 0; i < s.length(); i++) {
  
        // If current index is odd
        if (i % 2 == 1) {
  
            // Skip the character
            continue;
        }
  
        // Otherwise, append the
        // character
        new_string += s[i];
    }
  
    // Return the result
    return new_string;
}
  
// Driver Code
int main()
{
    string str = "abcdef";
  
    // Function call
    cout << removeOddIndexCharacters(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
  
class GFG {
  
    // Function to remove odd indexed
    // characters from a given string
    static String removeOddIndexCharacters(
        String s)
    {
  
        // Stores the resultant string
        String new_string = "";
  
        for (int i = 0; i < s.length(); i++) {
  
            // If the current index is odd
            if (i % 2 == 1)
  
                // Skip the character
                continue;
  
            // Otherwise, append the
            // character
            new_string += s.charAt(i);
        }
  
        // Return the modified string
        return new_string;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String str = "abcdef";
  
        // Remove the characters which
        // have odd index
        str = removeOddIndexCharacters(str);
        System.out.print(str);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
    
# Function to remove the odd
# indexed characters from a given string
def removeOddIndexCharacters(s): 
        
      
      
    # Stores the resultant string 
    new_s = "" 
    
    i = 0
    while i < len(s): 
    
        # If the current index is odd
        if (i % 2 == 1):
          
            # Skip the character
            i+= 1
            continue
    
        # Otherwise, append the 
        # character 
        new_s += s[i]
        i+= 1
          
    
    # Return the modified string 
    return new_s 
    
# Driver Code 
if __name__ == '__main__'
    str = "abcdef"
    
    # Remove the characters which 
    # have odd index 
    str = removeOddIndexCharacters(str
    print(str)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement 
// the above approach 
using System;
  
class GFG{ 
  
// Function to remove odd indexed 
// characters from a given string 
static string removeOddIndexCharacters(string s) 
      
    // Stores the resultant string 
    string new_string = ""
  
    for(int i = 0; i < s.Length; i++)
    
          
        // If the current index is odd 
        if (i % 2 == 1) 
  
            // Skip the character 
            continue
  
        // Otherwise, append the 
        // character 
        new_string += s[i]; 
    
  
    // Return the modified string 
    return new_string; 
  
// Driver Code 
public static void Main() 
    string str = "abcdef"
  
    // Remove the characters which 
    // have odd index 
    str = removeOddIndexCharacters(str); 
      
    Console.Write(str); 
}
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

ace

Time Complexity: O(N)
Auxiliary Space: O(N)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : sanjoy_62