Open In App

Remove odd indexed characters from a given string

Last Updated : 09 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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);
    }
}


Python3




# 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)


C#




// 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


Javascript




// JavaScript program to implement
// the above approach
 
// Function to remove the odd
// indexed characters from a given string
function removeOddIndexCharacters(s)
{
 
    // Stores the resultant string
    var new_string = "";
 
    for (var 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
     string str = "abcdef";
 
    // Function call
    console.log(removeOddIndexCharacters(str));
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)


Output: 

ace

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads