×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to remove alphabets from an alphanumeric string

In this C program, we are going to learn how to remove alphabets from a given alphanumeric string? If string contains alphabets and numbers both, this program will remove only alphabets from the string.
Submitted by IncludeHelp, on April 05, 2018

Problem statement

Given a string and we have to remove it all alphabets using C program.

If string contains alphabets and numbers both (or anything with the alphabets), program to check the alphabets, if found then program will remove them from the string.

Example

Input:
String is: "hello123"
Output:
Final string is: "123"

Input:
String is: "Call 100"
Output:
Final string is: "100"

Program to remove all alphabets from given alphanumeric string in C

/* C program to remove all the characters
 * from the alphanumeric string
 */

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

// This function removes all the chars
// used in the string passed and will leave
// only the alphanumeric characters
int RemoveChars(char *string) {
  int length = 0, i = 0, j = 0, k = 0;

  length = strlen(string);

  for (i = 0; i < length; i++) {
    for (j = 0; j < length; j++) {
      if ((string[j] >= 'a' && string[j] <= 'z') ||
          (string[j] >= 'A' && string[j] <= 'Z')) {
        for (k = j; k < length; k++) {
          string[k] = string[k + 1];
        }
        length--;
      }
    }
  }
}

// main function
int main() {
  char string[50] = {0};

  printf("\nEnter the string : ");
  gets(string);

  // pass the string to the function
  RemoveChars(string);
  printf("Final string is : %s", string);
  return 0;
}

Output

Run 1:
Enter the string : hello123 
Final string is : 123

Run 2:
Enter the string : Call 100 
Final string is : 100

C String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.