Computer Science, asked by sivasruthi20, 10 months ago

find the alphabet present at the location and determine the occurrence of same alphabet c promgram​

Answers

Answered by qwtiger
0

Explanation:

The problem states that we need to find the location and occurrence of an alphabet which is given by the user in a user given word.

So the logic is to take the input from the user and store it into an character array. Then take the input for the particular letter. Then traverse the character array to find the exact positions of that letter. The times it will be found we will increase the count for once so that we can get the number of occurrence.

Below is the C program.

#include <stdio.h>

#include <string.h>

 int main()

{

   char n, a[50];   //initialize the character array //

   int i, location= 0, count = 0;  

 /* now we will take the inputs from the user.*/

printf("Enter character: ");

   scanf("%c", &n);

   printf("Now enter the word: ");

   scanf("%s", a);

   printf("Positions of '%c' in %s are: ", n, a);

/* now traverse the array to find the locations*/

   for (i = 0; i < strlen(a); i++)  // strlen to calculate the length the of the array//

   {

       if (a[i] == n)  //check if the value at the particular location matches the character//

       {

           count = 1;

           printf("%d  ", i + 1);

           location++;

       }

   }

   if (count)

   {

       printf("\nCharacter '%c' occured for %d times.\n", n, location);

   }

   else

printf("nothing is found\n");

 return 0;

}

Similar questions