c++ string code-HELP

edited December 2004 in Internet & Media
hi,
Using the simple code where a=1, b=2, c=3 etc a word can be assigned a (non-unique) score. For example computer=105. I want to write a program that can count the number of words in a text file that have a score specified by the user of the program. I want to use the program to find the number of words in a file that have a score of exactly 100.

Any help would be much appreciated
Mike

Comments

  • Straight_ManStraight_Man Geeky, in my own way Naples, FL Icrontian
    edited December 2004
    Parse through the file, word by word. TEST for value EOF each time you find a word or string to calc, this is a special file end-point hex or binary or octal or decimal value (you can check for it in almost any math counting system). Calc the "values." Then, for each word, right after value calc, throw out the value if not equal (.NE.) to 100 (just ignore it, go to next word). OTOH, if value equals 100, then increment a numeric variable value (value of variable is set to zero when program workspace is intialized). So, when word one that matches to a value calc of 100 is found, your variable, say NUM_MATCHES, has one added to it, and because it is zero to start with you add one to it and then and then each time you find another match you add one. AFTER incrementing, see if
  • edited December 2004
    that's excellent help, thanks.
    Main problem is how I go about assigning the letters numbers, for example, a=1, b=1, c=1, etc. And then how to get the system to add these up to work out whether they equal 100 or not.
  • edited December 2004
    you need an array or a hash, some data structure to hold this info.

    maybe a 2 deminsional array?

    [1]
    [2]
    [c'][3]

    something like that would work

    you'll want a pretty good searching algorithm because you're going to have to traverse that array for every letter of every word. summing COULD get a little slow on a large text file.


    edit--

    if you have the time i'd highly suggest using a hashtable for this


    there

    i attached code for JHashtable.h and JHashtable.cpp. maybe you can find it handy
  • mondimondi Icrontian
    edited December 2004
    why not just get the ascii value and subtract 16 and 48 (for upper and lower case)... no arrays to fuss with, no searches, just simple math.

    m
  • edited December 2004
    with a data structure it'd be easier to define any value per letter you may want to implement, upper case or lower case. if the values you assign to each letter will follow the patterns of assignment in ASCII exactly, then you can do that.

    ooo and when you find a word with that value you could push the word down onto a stack along with the colXrow coordinates of the word in the text file, based on total line count and character width. that way the end user could precisely find each word in the original text file.
  • edited December 2004
    thanks for all your responses. problem is this is the first year i've studied c++, or programming of any sort.
    All we've learnt is structures, strings, classes, arrays, getline function, functions, inheritance, loops, pointers, three-vectors. ie, no hash tables or ascii value, what is an ascii value???
    what would be the best way to do this with my knowledge.
    Here are my thoughts;
    -an array to assign the numbers to letters
    -some form of loop to run through every word in the text file (not sure how this would work, any help would be much appreciated)
    -and then a count to count the number of letters with 100 points say
    thanks
  • CyrixInsteadCyrixInstead Stoke-on-Trent, England Icrontian
    edited December 2004
    I'd go the ascii route. Most efficient. Good tutorials can be found on about.comn programming and various other sites.

    ~Cyrix
Sign In or Register to comment.