> index_s1 = strlen(num_s1);
What's here?
- the last char of the string
- or the \0
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Perhaps?
index_s1 = strlen(num_s1) -1; /* needed for offset of subscript zero */
/* remove the = part of <= */
for (index_s2 = 0; index_s2 <= strlen(num_s1); index_s2++){
num_s2[index_s2] = num_s1[index_s1];
index_s1--;
}
/* don't forget the '\0' terminator */
num_s2[index_s2] = '\0';
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
This line of code is correct:
index_s1 = strlen(num_s1) - 1;
Without the " - 1" in there, you are going outside the bounds of the array. Arrays are zero based, so if the array has 12 elements, and you access array[12], of a char array, you will access an end of string char '\0', (if the compiler made it during initiation, ie:
char array[] = "How are you?";
will have strlen of 12 char's, 0 - 11. The array[12] will be the end of string marker char: '\0'.
If you filled the char array yourself, it may just have junk there, but anyway, the -1 in the above string, makes all the difference.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
> I assumed it was the last char of the string.
Maybe actually check your assumption?
By say printing it?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
[...] Though this goes beyond the scope of this thread, does anyone know why it's computing the wrong answer?
if (strcmp(num_s1, num_s2) == 0){
largest = x * y;
}
Just because its a palindrome doesn't mean it is the largest.
if( strcmp( num_s1, num_2 ) == 0 )
{
if ( largest < ( x * y ) )
{
largest = x * y;
}
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218