You should be doing something closer to this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
for(n = 0; n < r.length(); ++n)/*Use your origional strings instead of those char arrays*/
{/*NOTE: string.length() returns the number of letters but the array starts at zero*/
for(j = 0; j < q.length(); ++j)/*Go through each letter in q and match it against the letter in r*/
{/*NOTE: See the note above it also applies here*/
if(r[n] == q[j])/*If the letters match*/
{/*Do whatever*/}/*End of if(...)*/
}/*End of for(j = 0; j < q.length(); ++j)*/
}/*End of for(n = 0; n < r.length(); ++n)*/
/*Continue your code*/
|
At line 1 in my code we make sure that n is set to zero, I know you did that when you initialized it but I did it anyway out of habbit. We then check n agains then length of the word stored in your string 'r', this makes it so that it doesn't really matter what is entered for 'r' we check n against the length of it no matter what. Then we increment n.
NOTES: The integer 'n' is our place holder for the 'r' string, which is a dynamic array of chars. We are not using your 'c' or 'd' arrays because the string class has all of the functions we need for your task built in already. Remember that 'n' will not increment until the end of 'for(...)' loop.
Line 2 is something to keep in mind when using this in the future. If the 'r' string is 5 letters long then 'r.length()' will return 5 but checking anything against the 5 position in an array that only goes up to 4 is wrong.
Line 4 resets the 'j' integer to zero, this time we want to do it because the second time it executes and on it will have a value stored in it. We then compare the integer 'j' against the length of the 'q' string for the same reasons we do it above with the 'n' int and the 'r' string. When the code with in the loop makes a pass we increment 'j'.
NOTE: Notice how 'n' does not increment until after 'j' has counted all the way up to q.length() this ensures that we are checking each letter in r against each letter in 'q'. This is why it is important to reset 'j'.
Line 6 is our comparision. The only thing to note here is how I am using 'q' and 'r' as arrays, this is because the class 'string' is actually an array of chars with some cool additional stuff built on.