Remove all characters other than alphabets from string
- Difficulty Level : Easy
- Last Updated : 15 Jul, 2022
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}gh
Output : PraBHatkumarsinghTo 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 characters using string erase function.
C++
// CPP program to remove all the// characters other then alphabets#include <bits/stdc++.h>usingnamespacestd;// function to remove characters and// print new stringvoidremoveSpecialCharacter(string s){for(inti = 0; i < s.size(); i++) {// Finding the character whose// ASCII value fall under this// rangeif(s[i] <'A'|| s[i] >'Z'&&s[i] <'a'|| s[i] >'z'){// erase function to erase// the characters.erase(i, 1);i--;}}cout << s;}// driver codeintmain(){string s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);return0;}Java
// Java program to remove all the characters// other then alphabetsclassGFG{// function to remove characters and// print new stringstaticvoidremoveSpecialCharacter(String s){for(inti =0; i < s.length(); i++){// Finding the character whose// ASCII value fall under this// rangeif(s.charAt(i) <'A'|| s.charAt(i) >'Z'&&s.charAt(i) <'a'|| s.charAt(i) >'z'){// erase function to erase// the characters = s.substring(0, i) + s.substring(i +1);i--;}}System.out.print(s);}// Driver codepublicstaticvoidmain(String[] args){String s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);}}// This code is contributed by Rajput-JiPython3
# Python3 program to remove all the# characters other then alphabets# function to remove characters and# print new stringdefremoveSpecialCharacter(s):i=0whilei <len(s):# Finding the character whose# ASCII value fall under this# rangeif(ord(s[i]) <ord('A')orord(s[i]) >ord('Z')andord(s[i]) <ord('a')orord(s[i]) >ord('z')):# erase function to erase# the characterdels[i]i-=1i+=1("".join(s))# Driver Codeif__name__=='__main__':s="$Gee*k;s..fo, r'Ge^eks?"s=[iforiins]removeSpecialCharacter(s)# This code is contributed by Mohit KumarC#
// C# program to remove all the characters// other then alphabetsusingSystem;classGFG {// function to remove characters and// print new stringstaticvoidremoveSpecialCharacter(strings){for(inti = 0; i < s.Length; i++){// Finding the character whose// ASCII value fall under this// rangeif(s[i] <'A'|| s[i] >'Z'&&s[i] <'a'|| s[i] >'z'){// erase function to erase// the characters = s.Remove(i,1);i--;}}Console.Write(s);}// Driver codepublicstaticvoidMain(){strings ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);}}// This code is contributed by Sam007.Javascript
<script>// Javascript program to remove all the characters// other then alphabets// function to remove characters and// print new stringfunctionremoveSpecialCharacter(s){for(let i = 0; i < s.length; i++){// Finding the character whose// ASCII value fall under this// rangeif(s[i] <'A'|| s[i] >'Z'&&s[i] <'a'|| s[i] >'z'){// erase function to erase// the characters = s.substring(0, i) + s.substring(i + 1);i--;}}document.write(s);}// Driver codelet s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);// This code is contributed by unknown2108</script>OutputGeeksforGeeksTime complexity: O(N2) as erase() may take O(n) in the worst case. We can optimize the solution by keeping track of two indexes.
C++
// CPP program to remove all the// characters other then alphabets#include <bits/stdc++.h>usingnamespacestd;// function to remove characters and// print new stringvoidremoveSpecialCharacter(string s){intj = 0;for(inti = 0; i < s.size(); i++) {// Store only valid charactersif((s[i] >='A'&& s[i] <='Z') ||(s[i] >='a'&& s[i] <='z')){s[j] = s[i];j++;}}cout << s.substr(0, j);}// driver codeintmain(){string s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);return0;}Java
// Java program to remove all the// characters other then alphabetsclassGFG {// function to remove characters and// print new stringstaticvoidremoveSpecialCharacter(String str) {char[] s = str.toCharArray();intj =0;for(inti =0; i < s.length; i++) {// Store only valid charactersif((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 codepublicstaticvoidmain(String[] args) {String s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);}}// This code is contributed by 29AjayKumarPython3
# Python program to remove all the# characters other then alphabets# Function to remove special characters# and store it in another variabledefremoveSpecialCharacter(s):t=""foriins:# Store only valid charactersif(i >='A'andi <='Z')or(i >='a'andi <='z'):t+=i(t)# Driver codes="$Gee*k;s..fo, r'Ge^eks?"removeSpecialCharacter(s)# This code is contributed by code_freakC#
// C# program to remove all the// characters other then alphabetsusingSystem;publicclassGFG {// function to remove characters and// print new stringstaticvoidremoveSpecialCharacter(String str) {char[] s = str.ToCharArray();intj = 0;for(inti = 0; i < s.Length; i++) {// Store only valid charactersif((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 codepublicstaticvoidMain() {String s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);}}//This code is contributed by PrinciRaj1992Javascript
<script>// JavaScript program to remove all the// characters other then alphabets// function to remove characters and// print new stringfunctionremoveSpecialCharacter(str){let s = str.split("");let j = 0;for(let i = 0; i < s.length; i++) {// Store only valid charactersif((s[i] >='A'&& s[i] <='Z')|| (s[i] >='a'&& s[i] <='z')) {s[j] = s[i];j++;}}document.write((s).join("").substring(0, j));}// driver codelet s ="$Gee*k;s..fo, r'Ge^eks?";removeSpecialCharacter(s);// This code is contributed by rag2127</script>OutputGeeksforGeeksTime Complexity: O(n)
Approach : Using isalpha() method.
Iterate over the characters of the string and if the character is an alphabet then add the character to the new string.Finally, the new string contains only the alphabets of the given string.Python3
# Python program to remove all the# characters other then alphabets# Function to remove special characters# and store it in another variabledefremoveSpecialCharacter(s):t=""foriins:if(i.isalpha()):t+=i(t)s="$Gee*k;s..fo, r'Ge^eks?"removeSpecialCharacter(s)OutputGeeksforGeeksApproach: Without built-in methods.
Initialize an empty string, string with lowercase alphabets(la) and uppercase alphabets(ua). Iterate a for loop on string, if the character is in la or ua using in and not in operators concatenate them to an empty string. Display the string after the end of the loop.Python3
# Python program to remove all the# characters other then alphabets# Function to remove special characters# and store it in another variabledefremoveSpecialCharacter(s):t=""la="abcdefghijklmnopqrstuvwxyz"ua="ABCDEFGHIJKLMNOPQRSTUVWXYZ"foriins:if(iinlaoriinua):t+=i(t)s="$Gee*k;s..fo, r'Ge^eks?"removeSpecialCharacter(s)OutputGeeksforGeeks
This article is contributed by Prabhat Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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_upRecommended ArticlesPage :Article Contributed By :Improved By :Article Tags :Practice Tags :



.png)