Trying to find a C string segment that matches a key

  • Thread starter Thread starter chiurox
  • Start date Start date
  • Tags Tags
    String
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment involving C strings, specifically the task of matching a key phrase to segments of a C string based on word lengths. Participants explore the implementation details, potential issues in the code, and suggestions for improvement.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The initial post describes an approach to extract segments of a C string that match the word lengths of a given key.
  • One participant suggests writing pseudocode before coding, emphasizing that the correctness of the code depends on the intended goal.
  • Concerns are raised about the algorithm not accounting for punctuation in word definitions, which may affect matching.
  • Another participant advises using built-in string manipulation functions from the C library, such as strcmp, to simplify the implementation.
  • The original poster acknowledges the omission of variable declarations but claims the code compiles and runs correctly, stating that punctuation has been handled by cleaning the input.
  • The original poster expresses a desire not only to find matches but also to store the matching characters from the C string.

Areas of Agreement / Disagreement

Participants express differing views on the completeness and correctness of the original code. While some offer constructive feedback, there is no consensus on the best approach or the adequacy of the current implementation.

Contextual Notes

There are unresolved issues regarding variable declarations and the handling of punctuation, as well as the overall logic of the matching algorithm.

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

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K