C Program to Remove Characters in a String Except Alphabets

Write a C program to remove characters in a string except for alphabets. The for loop iterates the string characters, and the if statement looks for non-alphabets. If true, we skip that character to remove all the string letters except the alphabets.

#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);
}
Remove Characters in a String Except Alphabets

In this c program, we used another string to copy the result after removing all the characters except alphabets.

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

int main()
{
  char str[100], strAlphas[100];
  int i, j;

  char temp;

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

  int len = strlen(str) - 1;

  for (i = 0, j = 0; i < len; i++)
  {
    if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
    {
      strAlphas[j] = str[i];
      j++;
    }
  }
  strAlphas[j] = '\0';
  printf("\nBefore removing Non-Alphabets = %s\n", str);
  printf("After removing Non-Alphabets   = %s\n", strAlphas);
}
Enter A String to Remove Non-Alphabets = c 123 programs at ???? 12 me

Before removing Non-Alphabets = c 123 programs at ???? 12 me

After removing Non-Alphabets   = cprogramsatme

This removes characters in a string except alphabets example uses the isalpha method to look for the alphabet.

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

int main()
{
  char str[100], strAlphas[100];
  int i, j;

  char temp;

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

  int len = strlen(str) - 1;

  for (i = 0, j = 0; i < len; i++)
  {
    if (isalpha(str[i]))
    {
      strAlphas[j] = str[i];
      j++;
    }
  }
  strAlphas[j] = '\0';
  printf("\nBefore removing Non-Alphabets = %s\n", str);
  printf("After removing Non-Alphabets   = %s\n", strAlphas);
}
Enter A String to Remove Non-Alphabets = hello123how 6789

Before removing Non-Alphabets = hello123how 6789

After removing Non-Alphabets   = hellohow