Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Trying to find a C string segment that matches a key

  1. Nov 15, 2008 #1
    1. The problem statement, all variables and given/known data
    Hi, so I have this assignment, more like a project. There are many parts to it.
    Say there is a C String: "this does not match hi bartay"
    And I have a Key: "my secret"
    I need to match the key to a segment in C String that has exactly the same word lengths. In this case, "my secret" would match "hi bartay"

    3. The attempt at a solution

    So first I'm grabbing out of the C String the first same number of words as in the Key: "this does"
    Then I'm comparing the length of the first word in the C String with the first word in my Key. This won't match, so then it grabs "does not" etc etc...

    //storing the number of letters in each word of key
    int letterCounter=0;
    int pos=0;
    for (int k=0; key[k]!='\0'; k++)
    if(key[k]!=' ')

    //storing the number of letters in each word of C String
    int letterCounter2=0;
    for (int m=0; ctring[m] != '\0'; m++)
    if(ctring[m] != ' ')
    ctring[pos2] = letterCounter2;

    Is this code right? How do I compare the arrays and if they are not the same, how do I get to grab the next words?
  2. jcsd
  3. Nov 15, 2008 #2
    I suggest you write your pseudocode and present it for discussion before going into coding.
    Whether the code is right depends on what you'd like to achieve your goal.
    Having said that, it seems that a couple of remarks are in order:
    1. The algorithm does not take into account of punctuation marks in the definition of 'words'. Perhaps this is intended, as long as the same definition is used in the 'key'.
    2. pos2 and the arrays have not been declared or its value defined before first use.

    You have compiled an array to count the number of letters for each of the key and the given string.

    I think it will do you a lot of good to compile the program before going further, at least it will catch a few syntaxic errors or sometimes even logic errors.
    If you do not have one, Google "Dev C++" and I believe the download is free.
  4. Nov 15, 2008 #3
    Also don't forget to use all the nice string manipulation functions found in the C library. Take strcmp for instance: http://www.opengroup.org/onlinepubs/000095399/functions/strcmp.html

    Whatever book you have in C should have these functions in it and they make life a lot easier. The first rule in software is to not rewrite functionality that someone else has already written and debugged.
  5. Nov 15, 2008 #4
    oh yeah, I forgot to declare pos2.
    But it's because I've compiled my code already and it runs fine.
    It does not take into account punctuation because I've already dealed with that. I stripped off all non letter characters and got it cleaned up to only contain words and spaces separating each.

    I don't only want to compare where they match, but I also want to have an array store the matching characters in the C-string...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook