This is a C Program to print combination of two words of two given strings without any repetition.
This C Program prints combination of two words of two given strings without any repetition.
Take input from the user and perform string operations as shown in the program below.
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); }
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.
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.
$ 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
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Watch Advanced C Programming Videos
- Apply for C Internship
- Practice Computer Science MCQs
- Practice BCA MCQs
- Buy C Books