Remove special characters from String

Problem Statement:- Program to Remove special characters from String.

Sample Input/Output:-


Sample Input First:

Csinfo360.Com

Sample Output First: 

Csinfo360Com

Sample Input Second: 

Csinfo@#Java* string

Sample Output Second: 

CsinfoJavastring


Data requirement:-

   Input Data:- str

  Output Data:- str or Final_String (In Python)

  Additional Data:-i, j, len in Java/C++ or str2 in Python

Program in C

Here is the source code of the C Program to Remove special characters from String.

Code:

#include<stdio.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i,j;

     for (i = 0; str[i] != '\0'; ++i) {
     while (!(str[i] >= 'a' && str[i] <= 'z') && !(str[i] >= 'A' && str[i] <= 'Z') && !(str[i] >= '0' &&                        str[i] <= '9') && !(str[i] == '\0')) {
         for (j = i; str[j] != '\0'; ++j) {
            str[j] = str[j + 1];
         }
         str[j] = '\0';
      }
   }
       printf("After removing special character string is: %s\n",str);
}

Input/Output:
Enter your String:Csinfo360.Com
After removing special character string is: Csinfo360Com

Program in C++

Here is the source code of the C++ Program to Remove special characters from String.

Code:

#include<iostream>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    int i,j;

     for (i = 0; str[i] != '\0'; ++i) {

     while (!(str[i] >= 'a' && str[i] <= 'z') && !(str[i] >= 'A' && str[i] <= 'Z') && !(str[i] >= '0' &&                        str[i] <= '9') && !(str[i] == '\0')) {
         for (j = i; str[j] != '\0'; ++j) {
            str[j] = str[j + 1];
         }
         str[j] = '\0';
      }
   }
       cout<<"After removing special character letter string is: "<<str;
}

Input/Output:
Enter your String:Csinfo@#Java* string
After removing special character letter string is: CsinfoJavastring

Program in Java

Here is the source code of the Java Program to Remove special characters from String.

Code:

import java.util.Scanner;
public class p34 {

public static void main(String[] args) {

Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
str1+='\0';
int i,j;
char[] str=str1.toCharArray();
for (i = 0; str[i] != '\0'; ++i) {
    while (!(str[i] >= 'a' && str[i] <= 'z') && !(str[i] >= 'A' && str[i] <= 'Z') && !(str[i] >=                                  '0' && str[i] <= '9') && !(str[i] == '\0')) {
         for (j = i; str[j] != '\0'; ++j) {
            str[j] = str[j + 1];
         }
         str[j] = '\0';
      }
   }
System.out.println("After removing special character string is: ");
for (i = 0; str[i] != '\0';i++)
System.out.print(str[i]);
    cs.close();
}
}

Input/Output:
Enter your String:Csinfo@#Java* string
After removing special character letter string is: CsinfoJavastring

Program in Python

Here is the source code of the Python Program to Remove special characters from String.

Code:


str=input("Enter the String:")
str2 = []
i = 0
while i < len(str):
    ch = str[i]
    if (ch>='a' and ch <= 'z') or  (ch >= 'A' and ch<= 'Z') or (ch >= '0' and ch <= '9') or (ch == '\0'):
        str2.append(ch)
    i += 1
Final_String = ''.join(str2)
print("After removing special character letter string is:",Final_String)

Input/Output:
Enter the String:Csinfo360.Com
After removing special character string is: Csinfo360Com

More:-

C/C++/Java/Python Practice Question 


Post a Comment

0 Comments