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) {...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
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] == '/'){               ...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
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"));       ...
Debug code to get it to only count non alpha numeric values. Right now it always...
Debug code to get it to only count non alpha numeric values. Right now it always outputs zero. Why? please explain /*Problem 1*/ #include <stdio.h> #include <string.h> #include <stdlib.h> int loop_count; char scanned_line[100]; int non_alpha_num_checker = 0; int total; int main() { printf("Enter a line: "); fgets(scanned_line,sizeof scanned_line, stdin); for(loop_count = 0; loop_count != '\0'; loop_count++){ if(scanned_line[loop_count] >= 'A' && scanned_line[loop_count]<='Z') { continue; }    else if(scanned_line[loop_count] >= 'a' && scanned_line[loop_count]<='z') { continue; } else if(scanned_line[loop_count] >= '0' && scanned_line[loop_count]<='9')...
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...
C Code! dictionary.c, start by downloading the boilerplate1 code from Blackboard. Read the code, then complete...
C Code! dictionary.c, start by downloading the boilerplate1 code from Blackboard. Read the code, then complete it at all places indicated by TODO. For this second assignment, you do not receive precise specifications. This is on purpose: in the software industry, you’ll find a lot of cases when you need to finish code started by others, while not being given precise instructions on how the code works or is supposed to work. The comments in the code will help you,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT