Here is a source code of the C program to check if a given character is present in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Check whether a given Character is present in a
* String, Find Frequency & Position of Occurrence
char a, word;
int i, freq = 0, flag = 0;
printf("Enter character: ");
printf("Now enter the word: ");
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
if (word[i] == a)
flag = 1;
printf("%d ", i + 1);
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
$ cc charfrequency.c $ ./a.out Enter character: r Now enter the word: programming Positions of 'r' in programming are: 2 5 Character 'r' occured for 2 times.
Sanfoundry Global Education & Learning Series – 1000 C Programs.