-
July 6th, 2009, 02:57 PM
#1
Copy char array in reverse to another char array
I've been trying to do this Palindrome program without succes...
It basically copy an string array backwards to another array and check if both are equal or not...
Where did I went wrong?
The code is:
#include <iostream>
#include <string.h>
using namespace std;
void main ()
{
char String[81];
char StringReverse[81];
int j(0);
int i(0);
cout << "Enter a string: ";
cin.getline(String,81);
cout << "\n\n";
while (String != '\0');
{
StringReverse[j] = String[i];
i++;
j++;
}
if (StringReverse[81] != String[81])
cout << "The string " << StringReverse << " is not a Palindrome" << "\n\n"<< endl;
else
cout << "Congratulations! The string " << String << " is a Palindrome" <<endl;
}
-
July 6th, 2009, 03:01 PM
#2
Re: Copy char array in reverse to another char array
Code:
while (String != '\0');
{
StringReverse[j] = String[i];
i++;
j++;
}
What makes you think this loop will reverse the string? 'Cause it looks more like a simple copy to me. Also, the while condition is wrong---you probably meant while (String[ i ] != '\0').
Code:
if (StringReverse[81] != String[81])
If you're going to work with char arrays rather than std::strings, you have to use strcmp() to compare them---the above definitely won't do what you expect.
-
July 6th, 2009, 03:16 PM
#3
Re: Copy char array in reverse to another char array
But I need to do this without using "str" methods....
-
July 6th, 2009, 03:37 PM
#4
Re: Copy char array in reverse to another char array
His comments apply. Your code doesn't reverse a string, it copies it in the same order and your test for equality aside from not being how you compare strings, is looking at memory your app doesn't have access to.
Forgetting code for a minute, if you had to do it with a pencil and paper, how would you reverse a string?
-
July 6th, 2009, 03:42 PM
#5
Re: Copy char array in reverse to another char array
Originally Posted by thiemebr
But I need to do this without using "str" methods....
Then start with finding out, how long the string was that the user entered.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
July 6th, 2009, 03:55 PM
#6
Re: Copy char array in reverse to another char array
Originally Posted by GCDEF
His comments apply. Your code doesn't reverse a string, it copies it in the same order and your test for equality aside from not being how you compare strings, is looking at memory your app doesn't have access to.
Forgetting code for a minute, if you had to do it with a pencil and paper, how would you reverse a string?
I would reverse a string by copying the last char of an array to the 1st position of another or duplicating the same array and comparing the 1st char of array1 to the last char of array2...
-
July 6th, 2009, 04:38 PM
#7
Re: Copy char array in reverse to another char array
Either approach will work. The second is probably slightly more efficient since it can be done on a single array without copying.
-
July 6th, 2009, 05:05 PM
#8
Re: Copy char array in reverse to another char array
Originally Posted by thiemebr
I would reverse a string by copying the last char of an array to the 1st position of another or duplicating the same array and comparing the 1st char of array1 to the last char of array2...
Is your code doing either of those?
-
July 6th, 2009, 05:16 PM
#9
Re: Copy char array in reverse to another char array
Originally Posted by GCDEF
Is your code doing either of those?
I'm doing the single array checking the string... but still not work....
-
July 6th, 2009, 05:17 PM
#10
Re: Copy char array in reverse to another char array
I'm having problem to go to the end of the array and backward checking the string
-
July 6th, 2009, 05:19 PM
#11
Re: Copy char array in reverse to another char array
First things first: Find the length of the string and store it in a variable.
-
July 6th, 2009, 05:23 PM
#12
Re: Copy char array in reverse to another char array
OK..Done
for (count = 0; letter[count]!='\0'; count++);
-
July 6th, 2009, 05:26 PM
#13
Re: Copy char array in reverse to another char array
Good. Given that, how would you compare the last character to the first? Forget the rest for the moment.
-
July 6th, 2009, 05:40 PM
#14
Re: Copy char array in reverse to another char array
Going to the end of the array and doing the opposite of what I did
-
July 6th, 2009, 05:47 PM
#15
Re: Copy char array in reverse to another char array
Write an if statement which executes only if the first and last character are the same. It's one line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|