How Many Times?


General Statement: For an input string, determine how many times an input letter occurs in that string.

Input: There are 2 lines of data. The first line is the input string. The second line contains an unknown number of letters on a single line. An asterisk (*) is used to indicate the end of the data set.

Output: Output the letter, followed by an equal sign (=) and then the count for that letter.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
The maximum string length is 60.

Sample Input:
ALL IS QUIET NOW, BUT WAIT!
QAET*

Sample Output:
Q = 1
A = 2
E = 1
T = 3

Solutions :

#include <stdio.h>

int main ()
{
    char input [65];

    gets (input);

    char ch;

    while ( (ch = getchar ()) != '*' ) {
        int count = 0;

        for ( int i = 0; input [i] != 0; i++ ) {
            if ( input [i] == ch )
                count++;
        }

        printf ("%c = %d\n", ch, count);
    }

    return 0;
}
Advertisements

One thought on “How Many Times?

  1. A PROGRAM FOR THE ABOVE PROBLEM

    #include
    #include
    int main(){
    char string[60];
    char letter[60];
    int i,j,k;
    int length,len;
    int count=0;
    printf(“please enter a string: “);
    fgets(string,60,stdin);
    for(i=0;i<60;i++){
    if(string[i]=='\n'){
    string[i]='\0';
    }
    }
    printf("please enter a string of letters that ends with *: ");
    fgets(letter,60,stdin);
    for(i=0;i<60;i++){
    if(letter[i]=='\n'){
    letter[i]='\0';
    }
    }
    length=strlen(string)-1;
    len=strlen(letter)-2;
    for(i=0;i<=len;i++){
    for(j=0;j<=length;j++){
    if(letter[i]==string[j]){
    count=count+1;
    }
    }
    printf("%c=%d\n",letter[i],count);
    count=0;
    }
    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s