Trying to find a C string segment that matches a key

  • Thread starter chiurox
  • Start date
  • Tags
    String
In summary, the programmer is trying to match the key to a segment of a C string that has the same word lengths as the given key. The code does not take into account of punctuation marks, and the value of pos2 has not been declared.
  • #1
chiurox
35
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
  • #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.
 
  • #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.
 
  • #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...
 

Related to Trying to find a C string segment that matches a key

1. How do you determine the key of a C string segment?

To determine the key of a C string segment, you must first understand the fundamentals of music theory. The key of a C string segment refers to the main tonal center of the segment, or the note that the segment resolves to. This can be determined by analyzing the notes and chords used in the segment and identifying the dominant and tonic notes.

2. What is the difference between a C string segment and a key?

A C string segment refers to a specific section of musical notes played on a C string instrument, such as a guitar or violin. A key, on the other hand, refers to the tonal center or main note of a segment. While a C string segment may contain multiple key changes, the key remains constant throughout the segment.

3. How can you find a C string segment that matches a specific key?

The best way to find a C string segment that matches a specific key is by using a music theory tool, such as a chord finder or key analyzer. These tools can help you identify the key of a C string segment and find other segments that share the same key.

4. Can you transpose a C string segment to match a different key?

Yes, it is possible to transpose a C string segment to match a different key. This can be done by adjusting the notes and chords in the segment to match the new key. However, this may require some knowledge of music theory and transposition techniques.

5. Are there any specific techniques for finding a C string segment that matches a key in a specific genre?

While there are no specific techniques for finding a C string segment that matches a key in a specific genre, it may be helpful to listen to and analyze other songs in the same genre to get a better understanding of the common key changes and patterns used. Additionally, using a chord progression chart or key signature chart can also aid in finding matching segments in a specific genre.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
957
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
900
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top