📜  从给定的字符串删除奇数索引字符

📅  最后修改于: 2021-04-24 21:56:14             🧑  作者: Mango

给定大小为N的字符串str,任务是除去存在于给定的字符串的奇数索引(从0开始的索引)的字符。

例子 :

方法:请按照以下步骤解决问题:

  • 初始化一个空字符串,例如new_string,以存储结果。
  • 遍历给定的字符串,对于每个索引,检查它是否为偶数
  • 如果发现为真,则将这些索引处的字符附加到字符串new_string
  • 最后,在遍历整个字符串,返回new_string

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
  
#include 
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


输出:
ace

时间复杂度: O(N)
辅助空间: O(N)