C Program to Separate Special Characters and Alphabets

Introduction

In this post, I am going to write a c program to separate special characters and alphabets. Actually, I am also separating digits from the given string.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

I am assuming that you know what are alphabets, digits and special symbols. Before writing the program, let me show the expected output.

Expected Output

c program to separate special characters and alphabets
Output – c program to separate special characters and alphabets

For getting the above output, the following steps are very important to understand the program.

  1. Firstly, I am asking the user to enter a string.
  2. User can enter any string. Here, I have entered a mixed string to show you the complete output.
  3. Now, this string will be stored in the character array str.
  4. As I said above, this program will separate digits, alphabets and special symbols so I have taken three characters arrays for this i.e. d[100], al[100] and ss[100] respectively.
  5. I have also declared four integer variables. The variable i is the loop variable. The remaining variables like j, k and l are the counter variables for counting digits, alphabets and special symbols respectively.
  6. Using the while loop, each and every character is compared with the ASCII values of digits, alphabets and special symbols.
  7. For this, I have used an if-else statement to decide which is an alphabet, digit or special symbol.
  8. After deciding, I am storing that respective character in their respective array.

Also Read: C Program to Print Multiples of 5 using do-while loop

I hope you have understood the above steps. Now, see the following program.

C Program to Separate Special Characters and Alphabets

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char str[100];
    char ss[100],al[100],d[100];
    char ch;
    int i=0,j=0,k=0,l=0;
    printf("Enter a string: ");
    gets(str);
    while((ch=str[i])!='\0')
    {
        if((ch>=48)&&(ch<=57))
        {
           d[j]=ch;
           j++;
        }
        else if(((ch>=65)&&(ch<=90))||((ch>=97)&&(ch<=122)))
        {
           al[k]=ch;
           k++;
        }
        else
        {
            if(ch==32)
            {
                i++;
                continue;
            }
            ss[l]=ch;
            l++;
        }
        i++;
    }
    if(j!=0)
    printf("Digits in the given string are : ");
    for(i=0;i<j;i++)
    {
        printf("%c ",d[i]);
    }
    if(k!=0)
    printf("\nAlphabets in the given string are : ");
    for(i=0;i<k;i++)
    {
        printf("%c ",al[i]);
    }
    if(l!=0)
    printf("\nSpecial Symbols in the given string are : ");
    for(i=0;i<l;i++)
    {
        printf("%c ",ss[i]);
    }
    printf("\n\nTotal number of digits are : %d",j);
    printf("\nTotal number of alphabets are : %d",k);
    printf("\nTotal number of special symbols are : %d",l);
    return 0;
}

I hope you have understood the above program. If you have any problem, then please feel free to contact me.

Thank you.

Some Important C Programs

  1. C Program to Remove Zeros from a number
  2. The while loop in C Programming
  3. Reverse a Number using getchar and putchar function in c
  4. C Program to Print Numbers Except Multiples of n
  5. C Program to Remove White Spaces and Comments from a File
  6. Program in C to Find Longest Line in a File
  7. Palindrome in C using Pointers
  8. Insert and Delete element in Array in C using switch case
  9. C Program to Add Alternate Elements of a 2D Array
  10. Arrays in C for Complete Beginners
  11. C Program to Find Area of a Circle using Preprocessor

4 thoughts on “C Program to Separate Special Characters and Alphabets”

  1. Oh my goodness! Incredible article
    dude! Thanks, However I am going through problems
    with your RSS. I don’t know why I cannot subscribe
    to it. Is there anybody else getting
    identical RSS problems?
    Anyone who knows the solution will you kindly respond?

    Thanks!!

    Reply
  2. This program has lot of security issues… and very suboptimal. Does not work of any characters other than Latin A-Z, a-z, 0-9. Will fail for french or german characters. I am not even considering devanagari or arabic here.
    first of all your gets(str) is a big buffer overrun potential.

    Reply
    • Hi,
      Thank you for reading this article. I have considered only English alphabets while writing this program. Mainly I have used ASCII character for this. If you want to use any other language, then you will have to use respective ASCII characters or Unicode. This program will remain the same only the conditions in the if-else statement will be changed.
      Thank you.

      Reply

Leave a Comment