Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Write a program that takes a string as input and counts the frequency of each character. Display the results in alphabetical order.

#include <stdio.h>
#include <stdlib.h>

int main() {
// Declare variables
char inputString[100];
int frequency[26] = {0}; // Assuming input is in uppercase and considering only alphabetical characters

// Input: Get a string from the user
printf(“Enter a string: “);
fgets(inputString, sizeof(inputString), stdin);

// Process: Count character frequency
for (int i = 0; inputString[i] != ”; i++) {
if (inputString[i] >= ‘A’ && inputString[i] <= ‘Z’) {
frequency[inputString[i] – ‘A’]++;
}
}

// Output: Display character frequencies
printf(“Character frequencies in alphabetical order:n”);
for (int i = 0; i < 26; i++) {
if (frequency[i] > 0) {
printf(“%c: %dn”, ‘A’ + i, frequency[i]);
}
}

return 0; // Exit the program successfully
}