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 : PraBHatkumarsingh
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++
#include <bits/stdc++.h>
using namespace std;
void removeSpecialCharacter(string s)
{
for ( int i = 0; i < s.size(); i++) {
if (s[i] < 'A' || s[i] > 'Z' && s[i] < 'a'
|| s[i] > 'z' ) {
s.erase(i, 1);
i--;
}
}
cout << s;
}
int main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
return 0;
}
|
Java
class GFG
{
static void removeSpecialCharacter(String s)
{
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) < 'A' || s.charAt(i) > 'Z' &&
s.charAt(i) < 'a' || s.charAt(i) > 'z' )
{
s = s.substring( 0 , i) + s.substring(i + 1 );
i--;
}
}
System.out.print(s);
}
public static void main(String[] args)
{
String s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Python3
def removeSpecialCharacter(s):
i = 0
while i < len (s):
if ( ord (s[i]) < ord ( 'A' ) or
ord (s[i]) > ord ( 'Z' ) and
ord (s[i]) < ord ( 'a' ) or
ord (s[i]) > ord ( 'z' )):
del s[i]
i - = 1
i + = 1
print ("".join(s))
if __name__ = = '__main__' :
s = "$Gee*k;s..fo, r'Ge^eks?"
s = [i for i in s]
removeSpecialCharacter(s)
|
C#
using System;
class GFG {
static void removeSpecialCharacter( string s)
{
for ( int i = 0; i < s.Length; i++)
{
if (s[i] < 'A' || s[i] > 'Z' &&
s[i] < 'a' || s[i] > 'z' )
{
s = s.Remove(i,1);
i--;
}
}
Console.Write(s);
}
public static void Main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Javascript
<script>
function removeSpecialCharacter(s)
{
for (let i = 0; i < s.length; i++)
{
if (s[i] < 'A' || s[i] > 'Z' &&
s[i] < 'a' || s[i] > 'z' )
{
s = s.substring(0, i) + s.substring(i + 1);
i--;
}
}
document.write(s);
}
let s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
</script>
|
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)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void removeSpecialCharacter(string s)
{
int j = 0;
for ( int i = 0; i < s.size(); i++) {
if ((s[i] >= 'A' && s[i] <= 'Z' ) ||
(s[i] >= 'a' && s[i] <= 'z' ))
{
s[j] = s[i];
j++;
}
}
cout << s.substr(0, j);
}
int main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
return 0;
}
|
Java
class GFG {
static void removeSpecialCharacter(String str) {
char [] s = str.toCharArray();
int j = 0 ;
for ( int i = 0 ; i < s.length; i++) {
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));
}
public static void main(String[] args) {
String s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Python3
def removeSpecialCharacter(s):
t = ""
for i in s:
if (i > = 'A' and i < = 'Z' ) or (i > = 'a' and i < = 'z' ):
t + = i
print (t)
s = "$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
|
C#
using System;
public class GFG {
static void removeSpecialCharacter(String str) {
char [] s = str.ToCharArray();
int j = 0;
for ( int i = 0; i < s.Length; i++) {
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));
}
public static void Main() {
String s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Javascript
<script>
function removeSpecialCharacter(str)
{
let s = str.split( "" );
let j = 0;
for (let i = 0; i < s.length; i++) {
if ((s[i] >= 'A' && s[i] <= 'Z' )
|| (s[i] >= 'a' && s[i] <= 'z' )) {
s[j] = s[i];
j++;
}
}
document.write((s).join( "" ).substring(0, j));
}
let s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void removeSpecialCharacter(string s)
{
string ans = "" ;
for ( auto ch : s) {
if ( isalpha (ch))
ans += ch;
}
cout << ans;
}
int main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
return 0;
}
|
Java
public class Main {
static void removeSpecialCharacter(String s) {
String ans = "" ;
for ( char ch : s.toCharArray()) {
if (Character.isLetter(ch))
ans += ch;
}
System.out.println(ans);
}
public static void main(String[] args) {
String s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Python3
def removeSpecialCharacter(s):
t = ""
for i in s:
if (i.isalpha()):
t + = i
print (t)
s = "$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
|
C#
using System;
public class Program {
public static void Main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
RemoveSpecialCharacter(s);
}
public static void RemoveSpecialCharacter( string s)
{
string ans = "" ;
foreach ( char ch in s)
{
if (Char.IsLetter(ch))
ans += ch;
}
Console.WriteLine(ans);
}
}
|
Javascript
function removeSpecialCharacter(s) {
let ans = "" ;
for (let i = 0; i < s.length; i++) {
if (/[a-zA-Z]/.test(s[i])) {
ans += s[i];
}
}
console.log(ans);
}
let s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
|
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++
#include<bits/stdc++.h>
using namespace std;
string removeSpecialCharacter(string s) {
string t = "" ;
string la = "abcdefghijklmnopqrstuvwxyz" ;
string ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
for ( int i = 0; i < s.length(); i++) {
if (la.find(s[i]) != string::npos ||
ua.find(s[i]) != string::npos)
{
t += s[i];
}
}
cout << t << endl;
return t;
}
int main() {
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
return 0;
}
|
Java
import java.util.*;
class Gfg {
static String removeSpecialCharacter(String s)
{
String t = "" ;
String la = "abcdefghijklmnopqrstuvwxyz" ;
String ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
for ( int i = 0 ; i < s.length(); i++) {
if (la.contains(String.valueOf(s.charAt(i)))
|| ua.contains(
String.valueOf(s.charAt(i)))) {
t += s.charAt(i);
}
}
System.out.println(t);
return t;
}
public static void main(String[] args)
{
String s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Python3
def removeSpecialCharacter(s):
t = ""
la = "abcdefghijklmnopqrstuvwxyz"
ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in s:
if (i in la or i in ua):
t + = i
print (t)
s = "$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
|
C#
using System;
class Gfg{
static string removeSpecialCharacter( string s)
{
string t = "" ;
string la = "abcdefghijklmnopqrstuvwxyz" ;
string ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
for ( int i = 0; i < s.Length; i++)
if (la.Contains(s[i]) || ua.Contains(s[i]))
t += s[i];
Console.WriteLine(t);
return t;
}
public static void Main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Javascript
function removeSpecialCharacter( 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);
return t;
}
let s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
|
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.
C++
#include <bits/stdc++.h>
using namespace std;
void removeSpecialCharacter(string s)
{
string t = "" ;
for ( int i = 0; i < s.length(); i++) {
if ((s[i] >= 'a' && s[i] <= 'z' ) || (s[i] >= 'A' && s[i] <= 'Z' )) {
t += s[i];
}
}
cout << t << endl;
}
int main()
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void removeSpecialCharacter(String s) {
String t = "" ;
for ( int i = 0 ; i < s.length(); i++) {
if ((s.charAt(i) >= 'a' && s.charAt(i) <= 'z' ) || (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z' )) {
t += s.charAt(i);
}
}
System.out.println(t);
}
public static void main(String[] args) {
String s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
}
}
|
Python3
def removeSpecialCharacter(s):
t = ""
for i in s:
if ( ord (i) in range ( 97 , 123 ) or ord (i) in range ( 65 , 91 )):
t + = i
print (t)
s = "$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
|
C#
using System;
class GFG {
static void RemoveSpecialCharacter( string s)
{
string t = "" ;
for ( int i = 0; i < s.Length; i++) {
if ((s[i] >= 'a' && s[i] <= 'z' )
|| (s[i] >= 'A' && s[i] <= 'Z' )) {
t += s[i];
}
}
Console.WriteLine(t);
}
static void Main( string [] args)
{
string s = "$Gee*k;s..fo, r'Ge^eks?" ;
RemoveSpecialCharacter(s);
}
}
|
Javascript
function removeSpecialCharacter(s)
{
let t = ""
for (let i=0; i<s.length; i++)
if ((s[i]>= 'a' && s[i]<= 'z' ) || (s[i]>= 'A' && s[i]<= 'Z' ))
t+=s[i];
console.log(t);
}
let s = "$Gee*k;s..fo, r'Ge^eks?" ;
removeSpecialCharacter(s);
|
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!