The Student Room Group

C Programming HELP! <3

Hey guys, I've been trying to get this program working in Visual Studio 2013 using I think the GCC compiler (I'm new to all this) and I get a few errors I don't understand. If you are able to have a quick look and help me that would be brilliant.Context of the Code:I am writing a program that counts how many times a word(string) appears in another string e.g.How many times does Cars appear:"I only have 2 cars, but I want hundreds of cars.."twiceCode:https://www.dropbox.com/s/hcd4wt2153a6nrt/cword.c?dl=0
I can do it for you in Java if you want but i dont have experience in C
blind code:

int countshit(const char* str, const char* tofind)
{
int ret = 0;

for(;*str; ++str)
{
const char* cachetofind = tofind;
for(const char* cachestr = str; *cachestr== *cachetofind && *cachetofind && *cachestr; ++cachestr, ++cachetofind ) {}

ret += !*cachetofind ;
}

return ret;
}

//needs erorr cases:. note: returns 2 in following case findshit("sss", "ss":wink: not sure if correct
(edited 8 years ago)
Original post by CalvinCastle
Hey guys, I've been trying to get this program working in Visual Studio 2013 using I think the GCC compiler (I'm new to all this) and I get a few errors I don't understand. If you are able to have a quick look and help me that would be brilliant.Context of the Code:I am writing a program that counts how many times a word(string) appears in another string e.g.How many times does Cars appear:"I only have 2 cars, but I want hundreds of cars.."twiceCode:https://www.dropbox.com/s/hcd4wt2153a6nrt/cword.c?dl=0





It's quite annoying, you have to scan through the entire sentence using pointers and then compare it to an input

You should be using memory management for this.
Reply 4
Heres a different way to approach it:
Take the string and split it up into words by looking for the space character or string termination character '\0'.
Store each word in an array of char *.
Loop over the words in the array and compare with your search word.
Count up the matches

might be easier :smile:
(edited 8 years ago)
If you're interested in speeding up the solution look up Knuth Morris Pratt algorithm

Quick Reply

Latest

Trending

Trending