I'm having a bit of trouble reversing a string. Here's a code snippet of how I'm currently trying to do it:

index_s1 = strlen(num_s1);
for (index_s2 = 0; index_s2 <= strlen(num_s1); index_s2++){
    num_s2[index_s2] = num_s1[index_s1];
    index_s1--;
}

> index_s1 = strlen(num_s1);
What's here?
- the last char of the string
- or the \0

> index_s1 = strlen(num_s1);
What's here?
- the last char of the string
- or the \0

I assumed it was the last char of the string.

This is my whole code:

/* Project Euler: Problem 4
 * 
 * A palindromic number reads the same both ways. 
 * The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
 * 
 * Find the largest palindrome made from the product of two 3-digit numbers.
 */

#include <stdio.h>
#include <string.h>

int main(){
    char num_s1[7];
    char num_s2[7];
    int largest = 0;
    
    int x, y, index_s1, index_s2;
    for (x = 100; x <= 999; x++){
        for (y = 100; y <= 999; y++){
            sprintf(num_s1, "%i", x * y);
            index_s1 = strlen(num_s1);
            for (index_s2 = 0; index_s2 <= strlen(num_s1); index_s2++){
                num_s2[index_s2] = num_s1[index_s1];
                index_s1--;
            }
            printf("%s\t%s\n", num_s1, num_s2);
            if (strcmp(num_s1, num_s2) == 0){
                largest = x * y;
            }
        }
    }
    printf("The largest palindrome made from the product of two 3-digit numbers is %i", largest);
    return 0;
}

The first problem was that num_s2 was never assigned anything, meaing that my either my loop or my statement wasn't working. You can tell this because when I tell it to print num_s1 and num_s2 (which I've only included as a test), it only prints num_s1.

Changing the statement

index_s1 = strlen(num_s1);

to

index_s1 = strlen(num_s1) - 1;

wields, well... different results.

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';
}

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.

So the newly updated code seems to work fine, but gives the wrong answer! Though this goes beyond the scope of this thread, does anyone know why it's computing the wrong answer?

#include <stdio.h>
#include <string.h>

int main(){
    char num_s1[7];
    char num_s2[7];
    int largest = 0;
    
    int x, y, index_s1, index_s2;
    for (x = 100; x <= 999; x++){
        for (y = 100; y <= 999; y++){
            sprintf(num_s1, "%i", x * y);
            index_s1 = strlen(num_s1) - 1;
            for (index_s2 = 0; index_s2 < strlen(num_s1); index_s2++){
                num_s2[index_s2] = num_s1[index_s1];
                index_s1--;
            }
            num_s2[index_s2] = '\0';
            printf("%s\t%s\n", num_s1, num_s2);
            if (strcmp(num_s1, num_s2) == 0){
                largest = x * y;
            }
        }
    }
    printf("The largest palindrome made from the product of two 3-digit numbers is %i", largest);
    return 0;
}

EDIT: If you remove the printf() statement inside the loop, the program runs fast, btw.

> I assumed it was the last char of the string.
Maybe actually check your assumption?
By say printing it?

> I assumed it was the last char of the string.
Maybe actually check your assumption?
By say printing it?

I did. When I tested it with two digit numbers, it printed 5 digit numbers, strlen returned 5.

[...] 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;
    }
}

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;
    }
}

Aha! Odd that I'd overlook something like that. But thanks so much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.