Tuesday 5 June 2018

Program to count the number of alphabets occurring in all the given N strings

Common Alphabets in N strings

N strings are passed as inputs to the program. Each string will contain only the alphabets a-z in lower case. A given alphabet may be repeated any number of times. The program must print the count C of the alphabets that are present in all the N string values.

Boundary conditions:
2 <= N <= 500
Maximum length of each string = 1000

Program:

********
#include <stdio.h>

#include <string.h>


int main()
{
    char a[500][1000],test[500];
    int i,N,count=0,j,k,len;
    
    scanf("%d",&N);
    if (N>=2 && N<=500)
    {
        for(i=0;i<N;i++)
            scanf("%s",a[i]);
        i=0; j=0;
        len=strlen(a[0]);
       
        while (a[0][i]!='\0')
        {
            for(k=0;k<j;k++)
            {
                if (a[0][i]!=test[k])
                    continue;
                else
                    break;
            }
            if (k>=j)
                test[j++]=a[0][i];
            ++i;
        }
        test[j]='\0';
        printf("%s",test);
        i=0; count=0;
        while (test[i]!='\0')
        {
            for(j=1;j<N;j++)
            {
                if (strchr(a[j],test[i]))
                 continue;
                else
                  break;
            }
            if (j>=N)
                ++count;
            ++i;
        }
        printf("%d",count);
    }
            
   return 0;
}

Example Input/Output:
3
mnppqqr
ajkmnnm
poormanagement

Output:
2

Explanation:
Only two alphabets m and n are present in all the three strings.

No comments:

Post a Comment