Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Find the count of Alphabetic and Alphanumeric strings from given Array of Strings
In C programming, counting alphabetic and alphanumeric strings from an array involves checking each character in every string. An alphabetic string contains only letters (a-z, A-Z), while an alphanumeric string contains both letters and digits (0-9).
Syntax
int isAlphabetic(char str[]); int isAlphanumeric(char str[]); void countStrings(char arr[][MAX_LEN], int n);
Algorithm
The approach to solve this problem involves the following steps
- Step 1 Iterate through each string in the array
- Step 2 Check each character to determine if string is alphabetic or alphanumeric
- Step 3 Count occurrences and maintain frequency of each unique string
- Step 4 Display counts and frequencies
Example 1: Basic Implementation
Here's a complete program to count alphabetic and alphanumeric strings
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isAlphabetic(char str[]) {
for (int i = 0; i < strlen(str); i++) {
if (!isalpha(str[i])) {
return 0;
}
}
return 1;
}
int isAlphanumeric(char str[]) {
int hasDigit = 0, hasAlpha = 0;
for (int i = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
hasDigit = 1;
} else if (isalpha(str[i])) {
hasAlpha = 1;
} else {
return 0;
}
}
return hasDigit && hasAlpha;
}
void countStrings(char arr[][50], int n) {
int alphabeticCount = 0, alphanumericCount = 0;
printf("String analysis:<br>");
for (int i = 0; i < n; i++) {
if (isAlphabetic(arr[i])) {
alphabeticCount++;
printf(""%s" - Alphabetic<br>", arr[i]);
} else if (isAlphanumeric(arr[i])) {
alphanumericCount++;
printf(""%s" - Alphanumeric<br>", arr[i]);
} else {
printf(""%s" - Neither<br>", arr[i]);
}
}
printf("\nCounts: %d alphabetic, %d alphanumeric<br>",
alphabeticCount, alphanumericCount);
}
int main() {
char arr[][50] = {"hello", "abc123", "world", "test456", "programming"};
int n = 5;
printf("Input array: ");
for (int i = 0; i < n; i++) {
printf(""%s" ", arr[i]);
}
printf("<br><br>");
countStrings(arr, n);
return 0;
}
Input array: "hello" "abc123" "world" "test456" "programming" String analysis: "hello" - Alphabetic "abc123" - Alphanumeric "world" - Alphabetic "test456" - Alphanumeric "programming" - Alphabetic Counts: 3 alphabetic, 2 alphanumeric
Example 2: With Frequency Counting
This example counts frequencies of duplicate strings
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void countWithFrequency(char arr[][50], int n) {
int alphabeticCount = 0, alphanumericCount = 0;
int frequency[50] = {0};
int processed[50] = {0};
for (int i = 0; i < n; i++) {
if (processed[i]) continue;
int isAlpha = 1;
int isAlphanum = 0;
int hasDigit = 0, hasLetter = 0;
for (int k = 0; k < strlen(arr[i]); k++) {
if (isdigit(arr[i][k])) {
hasDigit = 1;
isAlpha = 0;
} else if (isalpha(arr[i][k])) {
hasLetter = 1;
} else {
isAlpha = 0;
hasDigit = hasLetter = 0;
break;
}
}
if (hasDigit && hasLetter) isAlphanum = 1;
frequency[i] = 1;
for (int j = i + 1; j < n; j++) {
if (strcmp(arr[i], arr[j]) == 0) {
frequency[i]++;
processed[j] = 1;
}
}
if (isAlpha) {
alphabeticCount += frequency[i];
} else if (isAlphanum) {
alphanumericCount += frequency[i];
}
}
printf("%d %d<br>", alphabeticCount, alphanumericCount);
for (int i = 0; i < n; i++) {
if (!processed[i] && frequency[i] > 0) {
printf(""%s": %d<br>", arr[i], frequency[i]);
}
}
}
int main() {
char arr[][50] = {"snmd", "nej7dnr", "snmd", "dltmdj", "lbwm2p6"};
int n = 5;
countWithFrequency(arr, n);
return 0;
}
3 2 "snmd": 2 "nej7dnr": 1 "dltmdj": 1 "lbwm2p6": 1
Key Points
- Use
isalpha()andisdigit()fromctype.hfor character validation - Alphabetic strings contain only letters (a-z, A-Z)
- Alphanumeric strings contain both letters and digits
- Use
strcmp()to find duplicate strings for frequency counting
Conclusion
Counting alphabetic and alphanumeric strings in C involves character-by-character validation using standard library functions. The frequency approach helps identify duplicate strings and their occurrence counts efficiently.
Advertisements
