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'; /*...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
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 running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h>...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h> int main() { printf("%d\n", parseint("32")); return 0; } int parseint(char *str) { int i=0; while(str[i] != '\0'){ return (str[i] - '0'); i++; } return 1;    }
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')...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT