Find the count of Alphabetic and Alphanumeric strings from given Array of Strings


The aim of this article is to implement a program to find the count of alphabetic and alphanumeric strings from a given array of strings.

As we know, a string is a group of characters that ends with the null character "\0" in C programming. Characters from the C string are kept in a character array. A C string differs from a character array in that it ends with the distinctive character "\0."

Input

arr[] = {“snmd”, “nej7dnr”, “snmd”, “dltmdj”, “lbwm2p6”}

Output

 3 2
“snmd”: 2
“nej7dnr”: 1
“dltmdj”: 1
“lbwn2p6”: 1

Explanation

The strings "snmd" and "dltmdjj" from the input array are the only ones that include solely alphabets; the remaining strings that is "nej7dnr”, "lbwm2p6" contain numbers. So these two strings are alphanumeric.

The total number of strings are three alphabetic and two alphanumeric. The frequency of the Alphabetic string "snmd" is 2 whereas the frequency of remaining all other strings is 1.

Input

arr[] = {“s2d9”, “nr9bdr”, “s2d9”, “smoddj”, “lenek9”}

Output

 1 4
“s2d9”: 2
“nr9bdr”: 1
“smoddjj”: 1
“lenek9”: 1

Explanation

The strings "smoddj" from the input array is the only ones that include alphabets; the remaining strings that is "s2d9", "nr9bdr" and "lenek9" contain numbers. So these three strings are alphanumeric.

The total number of strings are one alphabetic and four alphanumeric. The frequency of the Alphanumeric string "s2d9" is 2 whereas the frequency of all other strings is 1.

Input

arr[] = {“ghjdj”, “ghjdj”, “1g3thd”, “ghjdj”, “paj8sbe9”}

Output

3 2
“ghjdj”: 3
“1g3thd”: 1
“paj8sbe9”: 1

Explanation

The strings "ghjdjd" from the input array is the only one that include alphabets; the remaining strings that is, "1g3thd" , "paj8sbe9" contain numbers. So these two strings are alphanumeric.

The total number of strings are three alphabetic and two alphanumeric. The frequency of the Alphabetic string "ghjdj" is 3 whereas the frequency of remaining all other strings is 1.

Problem Statement

Implement a C program to find count of alphabetic and alphanumeric strings from given array of strings

Approach

The approach to solve this problem and find count of Alphabetic and Alphanumeric strings from a given Array of Strings is by the use of hashing technique.

Here is a glimpse of what the hashing technique is all about.

To easily locate a particular value within an array, the data structure uses hashing. To every component in the array, a distinct hash code is generated, and the hash code is then stored in place of the original element. This makes it possible to quickly look up values when looking for a particular one and makes it simple to see any duplication.

To quickly locate a particular value within an array, the data structure uses a method called hashing. For every component in the array, a distinct hash code is generated, and the hash code is then stored instead of the real element. This makes it possible to quickly look up values when looking for a particular one and makes it simple to see any duplication.

Algorithm

The algorithm to implement a program to Find the count of Alphabetic and Alphanumeric strings from given Array of Strings given below −

  • Step 1 − Implementing a Function to find the frequency.

  • Step 2 − Define two arrays mp1 and mp2 to store the frequencies of the Alphabetic and Alphanumeric strings respectively.

  • Step 3 − Define two integer variables c1 and c2 to count the frequencies.

  • Step 4 − Finally print the The frequency of alphabetic strings as well as the frequency of Alphanumeric string as the result.

Example (C Program)

Here is the C program implementation of the above written algorithm to find count of alphabetic and alphanumeric strings from given array of strings −

#include <stdio.h>
#include <string.h>
void findTheFrequency(char v[][50], int n) {
   int c1 = 0, c2 = 0;
   int mp1[50] = {0};
   int mp2[50] = {0};
   int isAlphabetic;
   for (int i = 0; i < n; i++) {
      isAlphabetic = 1;
      for (int j = 0; j < strlen(v[i]); j++) {
         if (v[i][j] >= '0' && v[i][j] <= '9') {
            isAlphabetic = 0;
            break;
         }
      }
      if (isAlphabetic) {
         c1++;
         mp1[i]++;
      } else {
         c2++;
         mp2[i]++;
      }
   }
   printf("%d %d
", c1, c2); for (int i = 0; i < n; i++) { if (mp1[i] > 0) { printf("%s: %d
", v[i], mp1[i]); } } for (int i = 0; i < n; i++) { if (mp2[i] > 0) { printf("%s: %d
", v[i], mp2[i]); } } } int main() { int N = 5; char arr[][50] = {"def", "huy7p", "xyz", "khbnjdj", "sndjd9o8"}; findTheFrequency(arr, N); return 0; }

Output

On execution, it will produce the following output −

3 2def: 1xyz: 1khbnjdj: 1huy7p: 1sndjd9o8: 1

Conclusion

Likewise, we can find count of alphabetic and alphanumeric strings from given Array of Strings The challenge of obtaining the find count of alphabetic and alphanumeric strings from given array of strings is resolved in this article.

Here C programming code as well as the algorithm to find the count of alphabetic and alphanumeric strings from given array of atrings are provided.

Updated on: 31-Oct-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements