Theory
Write a C program to search all occurrences of a character in a string.
/**
* C program to count occurrences of a word in a given string
*/
#include
#include
#define MAX_SIZE 100 // Maximum string size
/* Function declaration */
int countOccurrences(char * str, char * toSearch);
int main()
{
char str[MAX_SIZE];
char toSearch[MAX_SIZE];
int count;
/* Input string and word from user */
printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(toSearch);
count = countOccurrences(str, toSearch);
printf("Total occurrences of '%s': %d", toSearch, count);
return 0;
}
/**
* Get, total number of occurrences of a word in a string
*/
int countOccurrences(char * str, char * toSearch)
{
int i, j, found, count;
int stringLen, searchLen;
stringLen = strlen(str); // length of string
searchLen = strlen(toSearch); // length of word to be searched
count = 0;
for(i=0; i .... Show MoreFrom Strings in C programming · C programming
Write a C program to search all occurrences of a character in a string.
Write a C program to find length of a string.
Write a program in C to find the largest and smallest word in a string.
Write a C program to find maximum occurring character in a string.
Write a C program to find total number of vowels and consonants in a string.
Write a C program to compare two strings
Write a C program to copy one string to another string.
Write a C program to concatenate two strings.