954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do you reverse string elements

//My function is but the problem is that i dont have a proper working main function
void reverse(int a[], int n) { // function: Reverses the elements of a. // parameters: // a inout array of values. // n in number of values in a, a[0]..a[n-1] // returns: nothing for (int i=0; i

hui
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Sorry, but you can't really expect that someone will understand your question or function if you post like this.
Retry and please use CODE-tags, indent your code, post the complete code and ask a clear question.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

/*My function is but the problem is that i dont have a proper working main function*/

void reverse(int a[], int n) 
{ 
 
 for (int i=0; i<n/2; i++) { 
 
int temp = a[i];
 
 a[i] = a[n-i-1];
 
 a[n-i-1] = temp; 
 
} 
 
}
hui
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
the problem is that i dont have a proper working main function


I see...but there are more problems. I'm guessing that you want to reverse a string?? Have a look at this , last post. It's in dutch but I think you will find the code helpfull.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

#include <iostream>

using namespace std;

void ReverseString(char string1[], char reversed[]);

int main()
{
    const int MAX = 80;
    char string1[MAX];
    char reversed[MAX];
    cout << "Enter your string: ";
    cin.get(string1,MAX);

    ReverseString(string1, reversed);

    return 0;
}

void ReverseString(char string1[], char reversed[])
{
    int length = strlen(string1);

    for (int i=length-1, j=0; i>=0; i--, j++)
        reversed[j] = string1[i];

    for (i=0; i<length; i++)
        cout << reversed[i];

    cout << endl;
}
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 

Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

#include <iostream>

using namespace std;

void ReverseString(char string1[], char reversed[]);

int main()
{
    const int MAX = 80;
    char string1[MAX];
    char reversed[MAX];
    cout << "Enter your string: ";
    cin.get(string1,MAX);

    ReverseString(string1, reversed);

    return 0;
}

void ReverseString(char string1[], char reversed[])
{
    int length = strlen(string1);

    for (int i=length-1, j=0; i>=0; i--, j++)
        reversed[j] = string1[i];

    for (i=0; i<length; i++)
        cout << reversed[i];

    cout << endl;
}

You really should compile and test your code before posting. It contains several bugs that may just confuse the OP.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
void ReverseString(char string1[], char reversed[])
{
     int i, length = strlen(string1);
     for (int i=length-1, j=0; i>=0; i--, j++)
     reversed[j] = string1[i];
     for (i=0; i<length; i++)
     cout << reversed[i];
    cout << endl;
    cin.get();
     cin.get();
}


Here's the reviewed code from may4life.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Hmm, dont know whats going wrong.. I copied and pasted my code from here and still works no prob on my compiler :-S
I always test my code and post it with 0 errors and 0 warnings.. Maybe there might be a mix up with our compilers.. Sorry for anyone who had problems with my code..but they work for me :-S

may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You