Remove odd indexed characters from a given string - GeeksforGeeks

07 Sep, 2020
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: aceExplanation: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++
Java
Python3
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 stringstring 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 Codeint main(){    string str = "abcdef";      // Function call    cout << removeOddIndexCharacters(str);      return 0;}
// Java program to implement// the above approachimport 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 program to implement# the above approach    # Function to remove the odd# indexed characters from a given stringdef 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# 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
ace

Time Complexity: O(N)Auxiliary Space: O(N)
sanjoy_62
School Programming
Searching
Strings
Searching
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Data Types
Inline Functions in C++
Pure Virtual Functions and Abstract Classes in C++
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Search an element in a sorted and rotated array
Find the Missing Number