Remove all characters other than alphabets from string

Given a string consisting of alphabets and others characters, remove all the characters other then 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}gh
Output : PraBHatkumarsingh



To remove all the characters other than alphabets(a-z) && (A-Z), we just compare the character with the ASCII value and the character whose value does not lie in the range of alphabets, we remove those character using string erase function.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to remove all the
// characters other then alphabets
#include <bits/stdc++.h>
using namespace std;
  
// function to remove characters and
// print new string
void removeSpecialCharacter(string s)
{
    for (int i = 0; i < s.size(); i++) {
          
        // Finding the character whose 
        // ASCII value fall under this
        // range
        if (s[i] < 'A' || s[i] > 'Z' &&
            s[i] < 'a' || s[i] > 'z'
        {   
            // erase function to erase 
            // the character
            s.erase(i, 1); 
            i--;
        }
    }
    cout << s;
}
  
// driver code
int main()
{
    string s = "$Gee*k;s..fo, r'Ge^eks?"
    removeSpecialCharacter(s); 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove all the characters
// other then alphabets
  
class GFG 
{
      
    // function to remove characters and
    // print new string
    static void removeSpecialCharacter(String s)
    {
        for (int i = 0; i < s.length(); i++)
        {
  
            // Finding the character whose 
            // ASCII value fall under this
            // range
            if (s.charAt(i) < 'A' || s.charAt(i) > 'Z' &&
                    s.charAt(i) < 'a' || s.charAt(i) > 'z'
            
                  
                // erase function to erase 
                // the character
                s = s.substring(0, i) + s.substring(i + 1);
                i--;
            }
        }
        System.out.print(s);
    }
      
    // Driver code
    public static void main(String[] args)
    {
        String s = "$Gee*k;s..fo, r'Ge^eks?"
        removeSpecialCharacter(s);
    
  
// This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to remove all the characters
// other then alphabets
using System;
  
class GFG {
      
    // function to remove characters and
    // print new string
    static void removeSpecialCharacter(string s)
    {
        for (int i = 0; i < s.Length; i++)
        {
  
            // Finding the character whose 
            // ASCII value fall under this
            // range
            if (s[i] < 'A' || s[i] > 'Z' &&
                    s[i] < 'a' || s[i] > 'z'
            
                  
                // erase function to erase 
                // the character
                s = s.Remove(i,1);
                i--;
            }
        }
          
        Console.Write(s);
    }
      
    // Driver code
    public static void Main()
    {
        string s = "$Gee*k;s..fo, r'Ge^eks?"
        removeSpecialCharacter(s);
    
}
  
// This code is contributed by Sam007.

chevron_right



Output:

GeeksforGeeks

Time complexity of above code is O(n*n) as erase() may take O(n) in worst case. We can optimize the solution by keeping track of two indexes.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to remove all the
// characters other then alphabets
#include <bits/stdc++.h>
using namespace std;
  
// function to remove characters and
// print new string
void removeSpecialCharacter(string s)
{
    int j = 0;
    for (int i = 0; i < s.size(); i++) {
          
        // Store only valid characters
        if ((s[i] >= 'A' && s[i] <= 'Z') ||
            (s[i] >='a' && s[i] <= 'z'))
        
            s[j] = s[i];
            j++;
        }
    }
    cout << s.substr(0, j);
}
  
// driver code
int main()
{
    string s = "$Gee*k;s..fo, r'Ge^eks?"
    removeSpecialCharacter(s); 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove all the 
// characters other then alphabets
  
class GFG {
  
// function to remove characters and 
// print new string 
    static void removeSpecialCharacter(String str) {
        char[] s = str.toCharArray();
        int j = 0;
        for (int i = 0; i < s.length; i++) {
  
            // Store only valid characters 
            if ((s[i] >= 'A' && s[i] <= 'Z')
                    || (s[i] >= 'a' && s[i] <= 'z')) {
                s[j] = s[i];
                j++;
            }
        }
        System.out.println(String.valueOf(s).substring(0, j));
    }
  
// driver code 
    public static void main(String[] args) {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
  
// This code is contributed by 29AjayKumar 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// C# program to remove all the 
// characters other then alphabets
using System;
public class GFG {
   
// function to remove characters and 
// print new string 
    static void removeSpecialCharacter(String str) {
        char[] s = str.ToCharArray();
        int j = 0;
        for (int i = 0; i < s.Length; i++) {
   
            // Store only valid characters 
            if ((s[i] >= 'A' && s[i] <= 'Z')
                    || (s[i] >= 'a' && s[i] <= 'z')) {
                s[j] = s[i];
                j++;
            }
        }
        Console.WriteLine(String.Join("",s).Substring(0, j));
    }
   
// driver code 
    public static void Main() {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
 //This code is contributed by PrinciRaj1992

chevron_right


Output:

GeeksforGeeks

Time Complexity : O(n)

This article is contributed by Prabhat kumar singh. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.