Question

I'm trying to find a code to check the occurrences of the word or string I...

I'm trying to find a code to check the occurrences of the word or string

I wrote my own code but it's not working can you please help and fix my code

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str[MAX_SIZE];
char tosearch[MAX_SIZE];

printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(tosearch);


int cursor = 0;
int i = 0;
int stringLen = 0;
int searchLen = 0;
int count1;
int count2;

stringLen = strlen(str);
searchLen = strlen(tosearch);
count1 = 0;
count2 = 0;

for (cursor = 0; cursor <= stringLen; )
{


if (str[cursor] == tosearch[i])
{
count1++;
if (count1 == searchLen)
{
count2++;
i = 0;
count1 = 0;
cursor++;
break;
}
else
{
i++;
cursor++;
  
}
}
else
{
i = 0;
cursor++;

}
}


printf("Total occurrences of %d", count2);

return 0;
}

Homework Answers

Answer #1
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str[MAX_SIZE];
char tosearch[MAX_SIZE];

printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(tosearch);


int cursor = 0;
int i = 0;
int stringLen = 0;
int searchLen = 0;
int count1;
int count2;

stringLen = strlen(str);
searchLen = strlen(tosearch);
count1 = 0;
count2 = 0;

for (cursor = 0; cursor <= stringLen; )
{


if (str[cursor] == tosearch[i])
{
count1++;
if (count1 == searchLen)
{
count2++;
i = 0;
count1 = 0;
cursor++;
//break; // Commented this line to correct the code.
}
else
{
i++;
cursor++;

}
}
else
{
i = 0;
if(count1 == 0){
    cursor++;
}
count1=0;
}
}


printf("Total occurrences of %d", count2);

return 0;
}

You added a break statement inside the if statement which checks if count1 == searchLen. We go inside the if statement whenever we find the string to be searched. But if we put a break inside the if, we break out of the outer for loop and stop the search. So we get only one occurence even if there are more than one occurence as we stop searching as soon as we find one occurence due to the break statement. Commenting the break statement corrects the code.

In the last else statement, we do cursor++ only when count1 = 0, else we dont, as the current character should not be skipped as it can be the first character of the string to be searched.

I would be happy to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)

Happy Coding !!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE];...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1; int count2; stringLen = strlen(str); searchLen = strlen(tosearch); count1 = 0; count2 = 0; for (cursor = 0; cursor <= stringLen; ) { if (str[cursor] == tosearch[i]) { count1++; if (count1 == searchLen) {...
I am trying to write a program in C language but keep running into errors. Any...
I am trying to write a program in C language but keep running into errors. Any help would be awesome. here is my code I have so far. #include <stdio.h> #include <conio.h> #include <string.h> int main(){    int lenght, i = 0, state = 0;    char line[100];    printf("enter the string for checking of comment line: \n");    gets(line);    while(i < strline(line)){        switch(state){            case 0: if (line[i] == '/'){               ...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 you have the following code what edits can you do #include <stdio.h> #include <string.h> void StringChallenge(char str[]) {      // code goes...
I am trying to figure out how to find the most common word length in the...
I am trying to figure out how to find the most common word length in the british dictionary. I have inputted the text file for the british dictionary into my program. I tried a tedious way, but it doesn't seem effective. Can someone help me? String longestWord = "";        int maxLength =0;        int count1 =0, count2 =0, count3=0, count4=0, count5=0, count6=0, count7=0, count8=0, count9=0, count10=0;        Scanner scanFile = new Scanner (new File("BritishDictionary.txt"));       ...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() { char str[100]; /*Buffer to hold reversed string */ reverse("cat", str); /*Reverse the string "cat" */ printf("reverse(\"cat\")=%s\n", str); /*Display result */ reverse("noon", str); /*Reverse the string "noon" */ printf("reverse(\"noon\")=%s\n", str); /*Display result */ } reverse(char *before,...
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT