954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reversing A String

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--;
}
sinkingships7
Newbie Poster
14 posts since Mar 2008
Reputation Points: 13
Solved Threads: 2
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
> 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.

sinkingships7
Newbie Poster
14 posts since Mar 2008
Reputation Points: 13
Solved Threads: 2
 

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
 

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.

sinkingships7
Newbie Poster
14 posts since Mar 2008
Reputation Points: 13
Solved Threads: 2
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
> 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.

sinkingships7
Newbie Poster
14 posts since Mar 2008
Reputation Points: 13
Solved Threads: 2
 

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

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!

sinkingships7
Newbie Poster
14 posts since Mar 2008
Reputation Points: 13
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You