Friday, March 17, 2023

C Programming. Printing the occurrence of each alphabet in the text entered as command line arguments.

Printing the occurrence of each alphabet in the text entered as command line arguments.

// Program that prints a table indicating
// occurrence of each alphabet in the text entered as command line arguments.

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

int main(int argc, char **argv) {
    char s[100];
    strcpy(s, argv[1]);
    int n = strlen(s);
    int count;
    printf("Occurrence of each alphabet in given string \"%s\" is :\n");
    for (int i=0; i<n; i++){
        count = 1;
        if (s[i] >= 'A' && s[i]<='Z' || s[i] >= 'a' && s[i]<='z') {
            if (s[i]!='\0'){
                for (int j=i+1; j<n; j++){
                    if (s[i]==s[j]){
                        count++;
                        s[j]='\0';
                    }
                }
                printf("'%c' = %d\n", s[i], count);
            }
        }
    }
    return 0;
}

Output :

filename.exe "Hello World" Occurrence of each alphabet in given string "Hello World" is : 'H' = 1 'e' = 1 'l' = 3 'o' = 2 'W' = 1 'r' = 1 'd' = 1

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...