Computer Science, asked by muakanshakya, 1 year ago

\huge{\bold{\underline{\underline{Heya!!}}}}
___________________________


Ques:- WAP in Java to input a string .Count and print the occurrence of a word"yes" from the string.
_____________________________

Pls don't spam❌

Answers

Answered by purnitanath
5
Here is source code of the C Program to count occurrence of a substring in string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
* C Program To Count the Occurrence of a Substring in String
*/
#include <stdio.h>
#include <string.h>

char str[100], sub[100];
int count = 0, count1 = 0;

void main()
{
int i, j, l, l1, l2;

printf("\nEnter a string : ");
scanf("%[^\n]s", str);

l1 = strlen(str);

printf("\nEnter a substring : ");
scanf(" %[^\n]s", sub);

l2 = strlen(sub);

for (i = 0; i < l1;)
{
j = 0;
count = 0;
while ((str[i] == sub[j]))
{
count++;
i++;
j++;
}
if (count == l2)
{
count1++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times in %s", sub, count1, str);
}

Anonymous: c program not java
Answered by tharune
0

Answer:Here is source code of the C Program to count occurrence of a substring in string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*

* C Program To Count the Occurrence of a Substring in String

*/

#include <stdio.h>

#include <string.h>

char str[100], sub[100];

int count = 0, count1 = 0;

void main()

{

int i, j, l, l1, l2;

printf("\nEnter a string : ");

scanf("%[^\n]s", str);

l1 = strlen(str);

printf("\nEnter a substring : ");

scanf(" %[^\n]s", sub);

l2 = strlen(sub);

for (i = 0; i < l1;)

{

j = 0;

count = 0;

while ((str[i] == sub[j]))

{

count++;

i++;

j++;

}

if (count == l2)

{

count1++;

count = 0;

}

else

i++;

}

printf("%s occurs %d times in %s", sub, count1, str);

Similar questions