C Program to Remove Characters in a String Except Alphabets

Write a C program to remove characters in a string except 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 characters 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);
}
C Program to Remove Characters in a String Except Alphabets

In this c program, we used another string to copy the result string 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("\nThe Actual String before removing Non-Alphabets = %s\n", str);
  printf("The Final String after removing Non-Alphabets   = %s\n", strAlphas);
}
Enter A String to Remove Non-Alphabets = c 123 programs at ???? 12 me

The Actual String before removing Non-Alphabets = c 123 programs at ???? 12 me

The Final String after removing Non-Alphabets   = cprogramsatme

This c 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("\nThe Actual String before removing Non-Alphabets = %s\n", str);
  printf("The Final String after removing Non-Alphabets   = %s\n", strAlphas);
}
Enter A String to Remove Non-Alphabets = hello123how 6789

The Actual String before removing Non-Alphabets = hello123how 6789

The Final String after removing Non-Alphabets   = hellohow