Trying to find a C string segment that matches a key

  • Thread starter Thread starter chiurox
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
The discussion revolves around a programming assignment that requires matching segments of a C string to a key based on word lengths. The user is attempting to extract words from the C string and compare their lengths to those in the key, but is facing challenges with code implementation. Key points include the need to account for punctuation, the declaration of variables, and the importance of compiling code to catch errors. Suggestions are made to utilize existing string manipulation functions in C to simplify the process. The user also expresses a desire to store matching characters from the C string, indicating a need for further development in their approach.
chiurox
Messages
35
Reaction score
0

Homework Statement


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"

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]!=' ')
letterCounter++;
else
{
keyNumLetters[pos]=letterCounter;
pos++;
letterCounter=0;
}
}

//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] != ' ')
letterCounter2++;
else
{
ctring[pos2] = letterCounter2;
pos2++;
letterCounter2=0;
}
}

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?
 
Physics news on Phys.org
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.
 
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.
 
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...
 

Similar threads

Back
Top