top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C program to check whether a given character is present in a string, find frequency & position of occurrence?

0 votes
4,984 views

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

posted May 16, 2017 by Pooja Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include<stdio.h>
int main()
{
        char str[100],ch;
        int i=0,count=0;
        printf("Enter a character\n");
        scanf("%c",&ch);
        printf("Enter string\n");
        scanf("%s",str);
        printf("Position of '%c' in %s are : ",ch,str);
        for(;str[i];i++)
        {
                if(str[i]==ch)
                {
                        printf("%d ",i);
                        count++;
                }
        }
        printf("\nCharacter '%c' occured for %d times\n",ch,count);
}
answer May 16, 2017 by Chirag Gangdev
Similar Questions
–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

+2 votes

Output
Enter the string: Welcome to C Class!
Enter the word to insert: programming
Enter the position you like to insert: 3
The string after modification is

Welcome to C programming Class!

+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

...