CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Jul 2009
    Posts
    18

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

    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Jul 2009
    Posts
    18

    Re: Copy char array in reverse to another char array

    But I need to do this without using "str" methods....

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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?

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Copy char array in reverse to another char array

    Quote Originally Posted by thiemebr View Post
    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.

  6. #6
    Join Date
    Jul 2009
    Posts
    18

    Cool Re: Copy char array in reverse to another char array

    Quote Originally Posted by GCDEF View Post
    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...

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Copy char array in reverse to another char array

    Quote Originally Posted by thiemebr View Post
    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?

  9. #9
    Join Date
    Jul 2009
    Posts
    18

    Re: Copy char array in reverse to another char array

    Quote Originally Posted by GCDEF View Post
    Is your code doing either of those?

    I'm doing the single array checking the string... but still not work....

  10. #10
    Join Date
    Jul 2009
    Posts
    18

    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

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  12. #12
    Join Date
    Jul 2009
    Posts
    18

    Re: Copy char array in reverse to another char array

    OK..Done

    for (count = 0; letter[count]!='\0'; count++);

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  14. #14
    Join Date
    Jul 2009
    Posts
    18

    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

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured