To remove all the characters other than alphabets(a-z) && (A-Z), we just compare the character with the ASCII value, and for the character whose value does not lie in the range of alphabets, we remove those characters using string erase function.
Implementation:
C++
// CPP program to remove all the
// characters other than alphabets
#include <bits/stdc++.h>
usingnamespacestd;
// function to remove characters and
// print new string
voidremoveSpecialCharacter(string s)
{
for(inti = 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
intmain()
{
string s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
return0;
}
Java
// Java program to remove all the characters
// other than alphabets
classGFG
{
// function to remove characters and
// print new string
staticvoidremoveSpecialCharacter(String s)
{
for(inti = 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
publicstaticvoidmain(String[] args)
{
String s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to remove all the
# characters other than alphabets
# function to remove characters and
# print new string
defremoveSpecialCharacter(s):
i =0
whilei < len(s):
# Finding the character whose
# ASCII value fall under this
# range
if(ord(s[i]) < ord('A') or
ord(s[i]) > ord('Z') and
ord(s[i]) < ord('a') or
ord(s[i]) > ord('z')):
# erase function to erase
# the character
dels[i]
i -=1
i +=1
print("".join(s))
# Driver Code
if__name__ =='__main__':
s ="$Gee*k;s..fo, r'Ge^eks?"
s =[i fori ins]
removeSpecialCharacter(s)
# This code is contributed by Mohit Kumar
C#
// C# program to remove all the characters
// other than alphabets
usingSystem;
classGFG {
// function to remove characters and
// print new string
staticvoidremoveSpecialCharacter(strings)
{
for(inti = 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
publicstaticvoidMain()
{
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 than alphabets
// function to remove characters and
// print new string
functionremoveSpecialCharacter(s)
{
for(let 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.substring(0, i) + s.substring(i + 1);
i--;
}
}
document.write(s);
}
// Driver code
let s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
// This code is contributed by unknown2108
</script>
Output
w3wiki
Time complexity: O(N2) as erase() may take O(n) in the worst case. We can optimize the solution by keeping track of two indexes. Auxiliary Space: O(1)
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.
Implementation:
C++
// CPP program to remove all the
// characters other than alphabets
#include <bits/stdc++.h>
usingnamespacestd;
// function to remove characters and
// print new string
voidremoveSpecialCharacter(string s)
{
// Initialize an empty string
string ans = "";
for(autoch : s) {
// if the current character
// is an alphabet
if(isalpha(ch))
ans += ch;
}
cout << ans;
}
// driver code
intmain()
{
string s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
return0;
}
// this code is contributed by Rajdeep Mallick(rajdeep999)
Java
publicclassMain {
// function to remove characters and print new string
staticvoidremoveSpecialCharacter(String s) {
// Initialize an empty string
String ans = "";
for(charch : s.toCharArray()) {
// if the current character is an alphabet
if(Character.isLetter(ch))
ans += ch;
}
System.out.println(ans);
}
// driver code
publicstaticvoidmain(String[] args) {
String s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
}
}
Python3
# Python program to remove all the
# characters other than alphabets
# Function to remove special characters
# and store it in another variable
defremoveSpecialCharacter(s):
t =""
fori ins:
if(i.isalpha()):
t+=i
print(t)
s ="$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
C#
usingSystem;
publicclassProgram {
publicstaticvoidMain()
{
strings = "$Gee*k;s..fo, r'Ge^eks?";
RemoveSpecialCharacter(s);
}
// function to remove characters and print new string
publicstaticvoidRemoveSpecialCharacter(strings)
{
// Initialize an empty string
stringans = "";
foreach(charch ins)
{
// if the current character is an alphabet
if(Char.IsLetter(ch))
ans += ch;
}
Console.WriteLine(ans);
}
}
// This code is contributed by sarojmcy2e
Javascript
// function to remove characters and print new string
functionremoveSpecialCharacter(s) {
// Initialize an empty string
let ans = "";
for(let i = 0; i < s.length; i++) {
// if the current character is an alphabet
if(/[a-zA-Z]/.test(s[i])) {
ans += s[i];
}
}
console.log(ans);
}
// driver code
let s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
// This code is contributed by princekumaras
Output
w3wiki
Time Complexity: O(n) Auxiliary Space: O(n)
Approach: 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.
Implementation:
C++
// C++ program to remove all the
// characters other than alphabets
#include<bits/stdc++.h>
usingnamespacestd;
string removeSpecialCharacter(string s) {
string t = "";
string la = "abcdefghijklmnopqrstuvwxyz";
string ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(inti = 0; i < s.length(); i++) {
if(la.find(s[i]) != string::npos ||
ua.find(s[i]) != string::npos)
{
t += s[i];
}
}
cout << t << endl;
returnt;
}
intmain() {
string s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
return0;
}
// This code is contributed by shivamsharma215
Java
// Java program to remove all the
// characters other than alphabets
importjava.util.*;
classGfg {
staticString removeSpecialCharacter(String s)
{
// empty string to store the result
String t = "";
String la = "abcdefghijklmnopqrstuvwxyz";
String ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(inti = 0; i < s.length(); i++) {
// if it is a letter, add it to the result string
if(la.contains(String.valueOf(s.charAt(i)))
|| ua.contains(
String.valueOf(s.charAt(i)))) {
t += s.charAt(i);
}
}
System.out.println(t);
returnt;
}
// Driver Code
publicstaticvoidmain(String[] args)
{
String s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
}
}
Python3
# Python program to remove all the
# characters other than alphabets
# Function to remove special characters
# and store it in another variable
defremoveSpecialCharacter(s):
t =""
la="abcdefghijklmnopqrstuvwxyz"
ua="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
fori ins:
if(i inla ori inua):
t+=i
print(t)
s ="$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
C#
// C# program to remove all the
// characters other than alphabets
usingSystem;
classGfg{
staticstringremoveSpecialCharacter(strings)
{
stringt = "";
stringla = "abcdefghijklmnopqrstuvwxyz";
stringua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(inti = 0; i < s.Length; i++)
if(la.Contains(s[i]) || ua.Contains(s[i]))
t += s[i];
Console.WriteLine(t);
returnt;
}
publicstaticvoidMain()
{
strings = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
}
}
Javascript
// JS program to remove all the
// characters other than alphabets
functionremoveSpecialCharacter( s) {
let t = "";
let la = "abcdefghijklmnopqrstuvwxyz";
let ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(let i = 0; i < s.length; i++)
{
if(la.includes(s[i]) || ua.includes(s[i]))
{
t += s[i];
}
}
console.log(t);
returnt;
}
let s = "$Gee*k;s..fo, r'Ge^eks?";
removeSpecialCharacter(s);
// this code is contributed by poojaagarwal2.
Output
w3wiki
Time complexity: O(n), where n is the length of the input string s. This is because in the worst case, the loop in the removeSpecialCharacter function will have to iterate through all the characters in the string. Auxiliary Space: O(n), where n is the length of the final string without special characters. This is because the function creates a new string t to store only the alphabetic characters. The size of the string t will be at most equal to the size of the original string s.
Approach: Using ord() function.The ASCII value of the lowercase alphabet is from 97 to 122. And, the ASCII value of the uppercase alphabet is from 65 to 90.