-1

Can someone help me how to exchange the values in array A[10] and array B[10] in reverse order. Here is my code so far, also, what will my codes be if the required value is a number .. not a letter?

#include <iostream>
#include <iomanip>

using namespace std;
using std::setw;

void REVERSE(char *);

int A, B;

int main()
{

    cout << "word in A: " << endl;
    cin >> char A[];

    cout << "word in B: " << endl;
    cin >> char B[];


    REVERSE(A);
    REVERSE(B);

    return 0;
}

void REVERSE (char *A)
{

    int counter = 0;
    while(A[counter] != '\0')
        counter++;

    for(int i=counter-1;i>=0;i--)
        cout<<A[i];
    cout<<endl;
};


if(A>B)
{
    temp = A;
    A = B;
    B = temp;
}   
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
Sekia
  • 3
  • 2
  • 1
    Please format your code properly in order to make it readable. – Paul R Feb 29 '16 at 11:24
  • 5
    Start by throwing away this program, then [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over from the simple "hello world" program. – Some programmer dude Feb 29 '16 at 11:27
  • To begin with, you should use correct array or STL vector to store the input. Then it should be easy for you to think about how to reverse the vector. – vcp Feb 29 '16 at 11:28
  • vector? that's a tad bit heavy for this problem, std::string is built for these things. – Samer Tufail Feb 29 '16 at 11:39

3 Answers3

1
std::string stringA(A);
std::string stringB(B);

std::reverse(stringA.begin(), stringA.end());
std::reverse(stringB.begin(), stringB.end());

std::reverse - Reverse

Samer Tufail
  • 1,835
  • 15
  • 25
1
int *ptr=&a;
int i=0;

while(i < n-1)
{
     ptr++;
     i++;
}
i=0;

while(i <n-1)
{
    b[i]=*ptr;
    ptr--;
    i++;
}

//Hope this helps!!. If i have understood your question correctly Happy to help further.

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
1

There are several things wrong in your code.

You declared Aand B as integer values. However then you (probably) try to read them as an char[](character array). Apart from the fact that this line cin >> char[] A should give you a compiler error this doesn't even make sence. You can't store character values inside of an integer.

If you're trying to read a string or a character array then declare A and B as one.

Also the trailing if-statement at the end of your code will make your compiler complain. C++ is no scripting language. Statements won't be executed if they are not inside of a function.

The following program does what I think you try to approach:

#include <iostream>
#include <algorithm> // include algorithm to use std::reverse

using namespace std;

int main()
{
    string A, B;  // declare A and B to be a string, there is no need to declare them at global scope

    cout << "word in A: ";
    cin >> A;
    cout << "word in B: ";
    cin >> B;

    reverse(A.begin(), A.end()); // reverses A
    reverse(B.begin(), B.end()); // reverses B

    cout << A << " " << B << endl; // prompt A and B
} 

If you want to read integers and convert them to a string to reverse them, try the following:

#include <iostream>
#include <algorithm> // include algorithm to use std::reverse

using namespace std;

int main()
{
    int A, B;  // declare A and B to be a int

    cout << "word in A: ";
    cin >> A;
    cout << "word in B: ";
    cin >> B;

    string strA(to_string(A)); // convert A into a string
    string strB(to_string(B)); // convert B into a string

    reverse(strA.begin(), strA.end()); // reverses string version of A
    reverse(strB.begin(), strB.end()); // reverses string version of B

    cout << strA << " " << strB << endl; // prompt strA and strB
} 

Note: to use to_string() you need to use the c++11 standard

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • THIS is exactly the answer i was looking for tho the word in A and word in B should be switched .. ex if i have a "tree" in A and "home" in B, it should output A= emoh , B = eert. any idea how can i do that? – Sekia Feb 29 '16 at 11:56
  • Have a look at the first version I wrote. By declaring `A` and `B` as a `string` they are used to store, well, a string ( a sequence of characters ). using `cin` you get the user input. If the user enters a word. the program will reverse that and prompt it to the user. – muXXmit2X Feb 29 '16 at 12:00