Hey there! Sign in to join this conversationNew here? Join for free

C Programming HELP! <3 Watch

Announcements
    • Thread Starter
    Offline

    0
    ReputationRep:
    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
    Offline

    3
    ReputationRep:
    I can do it for you in Java if you want but i dont have experience in C
    Offline

    0
    ReputationRep:
    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" not sure if correct
    Offline

    3
    ReputationRep:
    (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.
    Offline

    17
    ReputationRep:
    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
    Offline

    5
    ReputationRep:
    If you're interested in speeding up the solution look up Knuth Morris Pratt algorithm
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Should Spain allow Catalonia to declare independence?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.