C Program to Count the Occurrence of Word in a String

This is a C Program to print combination of two words of two given strings without any repetition.

Problem Description

This C Program prints combination of two words of two given strings without any repetition.

Problem Solution

Take input from the user and perform string operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to print combination of two words of two given strings without any repetition. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Print Combination of two Words of two 
 * given Strings without any Repetition
 */
#include <stdio.h>
#include <string.h>
 
void main()
{
    char string[100], str[10], c[10];
    int z, occ = 0, i = 0, j = 0, count = 0, len = 0;
 
    printf("Enter a string:");
    scanf("%[^\n]s", string);
    printf("Enter the word to check its occurence:");
    scanf("%s", str);
    len = strlen(str);
    for (i = 0;string[i] != '\0';i++)
    {
        count = 0;
        for (j = 0, z = i;j < len; j++, z++)
        {
            c[j] = string[z];
            if (c[j] == str[j])
            {
                count++; /* Incrementing the count if the characters of the main string match with the characters of the given word */
            }
        }
        if (count == len && string[z] == ' ')
        {
            occ++;        /* Incrementing the occ if word matches completely and next character in string is space */
        }
    }
    printf("The number of occ is %d\n", occ);
}
Program Explanation

In this C program, we are reading a value of string using ‘string’ variable. Compute the length of the string using strlen() function for ‘str’ variable. For loop is used to find the combination of two words of two given strings without any repetition.

advertisement
advertisement

In for loop initialize the value of ‘i’ variable as 0. Check the condition that the value of ‘string[]’ array variable with base index of the value of ‘i’ variable is not equal to null. If the condition is true then execute the iteration of the loop, initialize the value of ‘count’ variable to 0.

In another for loop initialize the value of ‘j’ variable to 0 and the value of ‘z’ variable to the value of ‘i’ variable. Check the condition that the value of ‘j’ variable is less than the value of ‘len’ variable. If the condition is true then execute the loop. If condition statement is used to check that the characters in the main string match with the characters of the given word.

If the condition is true then execute the statement and increment the value of ‘count’ variable. Another if condition statement is used to check that word matches completely and next character in a string is space is not to be true using logical AND operator. If the condition is true then execute the statement and increment the value of ‘occ’ variable. Print combination of two words of two given strings without any repetition.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
$ cc string3.c
$ a.out
Enter a string:welcome to sanfoundry's c programming class,  welcome again to c class
Enter the word to check its occurence:welcome
The number of occ is 2
 
$ cc string3.c
$ a.out
Enter a string:welcome to sanfoundry's c programming class,  welcome again to c class
Enter the word to check its occurence:c
The number of occ is 2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.