Reverse array in C++

Discussion in 'C++' started by j+e, Feb 18, 2009.

  1. j+e

    j+e New Member

    Joined:
    Feb 18, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thank you very much! That helped, because I found out that the letters where shuffled to EDCBA, and then shuffled back to ABCDE. So I just said that:

    for (int i = 0; i < (b-((b+1)/2)); i++)

    because odd number/2 will be rounded down, this works!

    Maybe not the best way to do it in a programmers view, but it worked :)
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > Maybe not the best way

    On the contrary, reversing a string by only looping over the first half, rounded down, is very efficient.
    Just to make sure though, try a even number length string, e.g. ABCDEFGH, make sure you get the correct output.
     
  3. j+e

    j+e New Member

    Joined:
    Feb 18, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I have tried that, and it works out perfectly well. Even though setences!! :happy:

    Again, thanks for your help!
     
  4. ijax

    ijax New Member

    Joined:
    Oct 26, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The code you guys entered is not correct,,, cause you have to reverse it along with its place,,,
    here this may be helpful

    #include<iostream.h>
    #include<conio.h>
    using namespace std;
    main()
    {
    cout<<"Enter The Number Alphabets your Word has \t";
    int x;
    cin>>x;
    cout<<"Enter Your Word:";
    char a[x];
    cin>>a;


    char temp;

    for (int i=x; i>=0;i--)
    {
    temp=a;
    a=temp;
    cout<<a;
    }
    cout<<endl;
    getch();
    }
     
  5. mukeshsoftona

    mukeshsoftona Banned

    Joined:
    Oct 28, 2011
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    It's correct coding dear. it's run i am sure.
    Thanks for sharing it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice