Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Searching Algorithms
  • MCQs on Searching Algorithms
  • Tutorial on Searching Algorithms
  • Linear Search
  • Binary Search
  • Ternary Search
  • Jump Search
  • Sentinel Linear Search
  • Interpolation Search
  • Exponential Search
  • Fibonacci Search
  • Ubiquitous Binary Search
  • Linear Search Vs Binary Search
  • Interpolation Search Vs Binary Search
  • Binary Search Vs Ternary Search
  • Sentinel Linear Search Vs Linear Search
Open In App
Next Article:
Remove "b" and "ac" from a given string
Next article icon

Remove odd indexed characters from a given string

Last Updated : 09 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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)



Next Article
Remove "b" and "ac" from a given string
author
jana_sayantan
Improve
Article Tags :
  • DSA
  • School Programming
  • Searching
  • Strings
Practice Tags :
  • Searching
  • Strings

Similar Reads

  • Remove a Character from a Given Position
    Given a string and a position (0-based indexing), remove the character at the given position. Examples: Input : s = "abcde", pos = 1Output : s = "acde"Input : s = "a", pos = 0Output : s = "" Approach - By Using Built-In MethodsWe use erase in C++, string slicing in Python, StringBuilder Delete in Ja
    5 min read
  • Find last index of a character in a string
    Given a string str and a character x, find last index of x in str. Examples : Input : str = "geeks", x = 'e' Output : 2 Last index of 'e' in "geeks" is: 2 Input : str = "Hello world!", x = 'o' Output : 7 Last index of 'o' is: 7 Recommended PracticeLast index of a character in the stringTry It! Metho
    8 min read
  • Find Minimum Indexed Character in String
    Given a string str and another string patt. The task is to find the character in patt that is present at the minimum index in str. If no character of patt is present in str then print "$". Examples: Input: str = "geeksforgeeks", patt = "set" Output: e Both e and s of patt are present in str, but e i
    14 min read
  • Remove "b" and "ac" from a given string
    Given a string, eliminate all "b" and "ac" in the string, you have to replace them in-place, and you are only allowed to iterate over the string once. (Source Google Interview Question) Examples: acbac ==> "" aaac ==> aa ababac ==> aa bbbbd ==> dWe strongly recommend that you click here
    12 min read
  • Remove even frequency characters from the string
    Given a string 'str', the task is to remove all the characters from the string that have even frequencies. Examples: Input: str = "aabbbddeeecc" Output: bbbeee The characters a, d, c have even frequencies So, they are removed from the string. Input: str = "zzzxxweeerr" Output: zzzweee Approach: Crea
    8 min read
  • Maximize characters to be removed from string with given condition
    Given a string s. The task is to maximize the removal of characters from s. Any character can be removed if at least one of its adjacent characters is the previous letter in the Latin English alphabet. Examples: Input: s = "bacabcab"Output: 4Explanation: Following are the removals that maximize the
    6 min read
  • Program for removing i-th character from a string
    Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a
    5 min read
  • Remove duplicates from a string
    Given a string s which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string. Note: The order of remaining characters in the output should be the same as in the original string. Example: Input: s = geeksforgeeksOu
    10 min read
  • Remove all occurrences of a character from a string using STL
    Given a string S and a character C, the task is to remove all the occurrences of the character C from the given string. Examples: Input:vS = "GFG IS FUN", C = 'F' Output:GG IS UN Explanation: Removing all occurrences of the character 'F' modifies S to "GG IS UN". Therefore, the required output is GG
    2 min read
  • Remove all characters other than alphabets from string
    Given a string consisting of alphabets and others characters, remove all the characters other than alphabets and print the string so formed. Examples: Input : $Gee*k;s..fo, r'Ge^eks?Output : GeeksforGeeks Input : P&ra+$BHa;;t*ku, ma$r@@s#in}ghOutput : PraBHatkumarsingh Recommended PracticeRemove
    13 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences