Not Yet Answered # Reversing A String

Discussion Starter sinkingships7 3 Aia 1,977 Adak 419 Discussion Starter sinkingships7 3 Salem 5,138 Discussion Starter sinkingships7 3 Aia 1,977 Discussion Starter sinkingships7 3 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

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.

0

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

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.

0

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.

0

> I assumed it was the last char of the string.

Maybe actually check your assumption?

By say printing it?

0

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.

0

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

0

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!

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...