To remove characters in string without including alphabets

To remove characters in string without including alphabets

Question:

Write a program to remove characters in the string without including alphabets.

Logic:

  • Get the input string from the user by including symbols.
  • Checks for non-alphabets in the string and removes it.
  • Then prints the remaining characters.

Algorithm:

  1. Get the input string from the user.
  2. Then check the individual characters in the string for non-alphabets.
  3. And removes the character in the string using swapping.
  4. Then print the remaining characters in the string.

Program:

[code lang=”c”]
#include<stdio.h>
void main()
{
char a[100];
int i,j,n;
clrscr();
printf("enter the string:");
gets(a);
n=strlen(a);
for(i=0;i<n;i++)
{
if((a[i]>65&&a[i]<91)||(a[i]>96&&a[i]<123))//checks for the alphabet
{
continue;
}
else
{
for(j=i;j<n;j++)//removes the character other than alphabet
{
a[j]=a[j+1];
}n–;//after removing decrementing the length
}
}
printf("string after removing characters:");
puts(a);//prints the string
getch();
}
[/code]

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *