Turn on thread page Beta
    • 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

    12
    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

    13
    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

    13
    ReputationRep:
    If you're interested in speeding up the solution look up Knuth Morris Pratt algorithm
 
 
 

University open days

  1. University of Bradford
    University-wide Postgraduate
    Wed, 25 Jul '18
  2. University of Buckingham
    Psychology Taster Tutorial Undergraduate
    Wed, 25 Jul '18
  3. Bournemouth University
    Clearing Campus Visit Undergraduate
    Wed, 1 Aug '18
Poll
How are you feeling in the run-up to Results Day 2018?

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

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