HELP C++ program driving me nuts!!!

So I almost got this program to work but i can seem to figure out the last part. I have to ask the user to input a string and then return the string in reverse. It has to be two functions, the main one asking user for input and then invoking the second function which reverses it. This is my code so far, help please.


#include <stdio.h>
int main(void){
char s[]="test string";
int b ,e,t;
e=strlen(s)-1;// its -1 since s[e] points to the terminating 0
for(b=0;b<e;b++,e--){
t=s[b];
s[b]=s[e];
s[e]=t;
}
printf("%s\n",s);
return 0;
}
This is a C program, not a C++ program.

Do you want help with C, or C++? Make a decision.
C i guess, whatever will make this code work.
In C:

http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c

In C++:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string str;
    std::getline(std::cin, str);
    std::reverse(str.begin(), str.end());
    std::cout << str << std::endl;
}
Last edited on
Topic archived. No new replies allowed.