C Program to Remove All Characters in a String Except Alphabets

C Program to Remove All Characters in a String Except Alphabets

C program to remove all characters in a string except alphabets; Through this tutorial, we will learn how to remove all characters in a string except alphabets using for loop and while loop in c programs.

Programs and Algorithm to Remove All Characters in a String Except Alphabets in C

  • Algorithm to Remove All Characters in a String Except Alphabets
  • C Program to Remove All Characters in a String Except Alphabets using For Loop
  • C Program to Remove All Characters in a String Except Alphabets using For and While Loop

Algorithm to Remove All Characters in a String Except Alphabets

Use the following algorithm to write a program to remove all characters in a string except alphabets; as follows:

  • Initialize the variables.
  • Accept the input from user.
  • Initialize a for or while loop.
  • Iterate each character through the loop.
  • Remove non alphabetical characters
  • Terminate for or while loop.
  • Print result.

C Program to Remove All Characters in a String Except Alphabets using For Loop

#include <stdio.h>
#include <string.h>

int main()
{
  char strAlphas[100];

  printf("Enter A String to Remove Non-Alphabets = ");
  fgets(strAlphas, sizeof strAlphas, stdin);

  int len = strlen(strAlphas);

  for (int i = 0; i < len; i++)
  {
    if (!(strAlphas[i] >= 'a' && strAlphas[i] <= 'z') || (strAlphas[i] >= 'A' && strAlphas[i] <= 'Z'))
    {
      for (int j = i; j < len; j++)
      {
        strAlphas[j] = strAlphas[j + 1];
      }
      len--;
      i--;
    }
  }

  printf("The Final String after Sorting Alphabetically = %s\n", strAlphas);
}

The output of the above c program; as follows:

Enter A String to Remove Non-Alphabets = [email protected]
The Final String after Sorting Alphabetically = hello

C Program to Remove All Characters in a String Except Alphabets using For and While Loop

#include <stdio.h>
int main()
{
    //Initializing variable.
    char str[100];
    int i, j;
    
     //Accepting input.
    printf(" Enter a string : ");
    gets(str);

     //Iterating each character and removing non alphabetical characters.
    for(i = 0; str[i] != '
#include <stdio.h>
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
printf(" Enter a string : ");
gets(str);
//Iterating each character and removing non alphabetical characters.
for(i = 0; str[i] != '\0'; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0') )
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0'; 
}
}
//Printing output.
printf(" After removing non alphabetical characters the string is :");
puts(str);
return 0;
}
'; ++i) { while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '
#include <stdio.h>
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
printf(" Enter a string : ");
gets(str);
//Iterating each character and removing non alphabetical characters.
for(i = 0; str[i] != '\0'; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0') )
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0'; 
}
}
//Printing output.
printf(" After removing non alphabetical characters the string is :");
puts(str);
return 0;
}
') ) { for(j = i; str[j] != '
#include <stdio.h>
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
printf(" Enter a string : ");
gets(str);
//Iterating each character and removing non alphabetical characters.
for(i = 0; str[i] != '\0'; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0') )
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0'; 
}
}
//Printing output.
printf(" After removing non alphabetical characters the string is :");
puts(str);
return 0;
}
'; ++j) { str[j] = str[j+1]; } str[j] = '
#include <stdio.h>
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
printf(" Enter a string : ");
gets(str);
//Iterating each character and removing non alphabetical characters.
for(i = 0; str[i] != '\0'; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0') )
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0'; 
}
}
//Printing output.
printf(" After removing non alphabetical characters the string is :");
puts(str);
return 0;
}
'; } } //Printing output. printf(" After removing non alphabetical characters the string is :"); puts(str); return 0; }

The output of the above c program; as follows:

Enter a string : [email protected]
After removing non alphabetical characters the string is :hello

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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