Checking the number of letters repeated in a word.


#include<stdio.h>
#include<string.h>
void main()
{
int i,count=0;
char c,str[100];
printf("Enter a sentence\n");
scanf("%c",&str);
printf("Enter a character to know it's repetance in sentence\n");
scanf("%c",&c);

for(i=0;i<strlen(str);i++)
{
    if(str[i]==c)
    {
        count++;
    }
}
printf("Letter %c repeated %d times\n",c,count);
getch();  }
Explanation:

  1. This program starts with initializing :
    • str[100] → To store string with length of 100 which means it can store 100 letters
    • i →Used as helping variable
    • count→ To store number of  times a letter is repeated in a given string and is initilized to 0.
    • →Used to store required letter
  2. printf("Enter a sentence\n");
    scanf("%c",&str);
    Takes string from user.
  3. printf("Enter a character to know it's repetance in sentence\n");
    scanf("%c",&c);
    Takes a letter from user for which we want to know number of times it repeated
  4. for(i=0;i<strlen(str);i++)
    {
        if(str[i]==c)
        {
            count++;
        }
    }

Comments

Popular posts from this blog

Privacy Policy for Constitution of Nepal

Privacy Policy for Noida Metro Fare

Privacy Policy for NepBrain