reverse name ??

hello
i write a program to take two user inputs. First input is your first name, and the second input is your family name. and check if both the inputs are same or not.
it work fine this

#include <iostream>
#include <cstring>
using namespace std;

int main() {
char firstName[20];
char familyName[20];
char fullName[40];

cout << "Enter your first name" << endl;
cin >> firstName;
cout << "Enter your family name" << endl;
cin >> familyName;

bool check = strcmp(firstName, familyName);
if (!check) {
cout << "Error - You have input same string as first and family name";
} else {
strcpy(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, familyName);
cout << "Your full name is " << fullName << endl;
}

return 0;
}

now i want the name print out in reverse, the full name in reverse order by reversing the order of characters.
how can i do it ??
first of all why are you using C stuff ?
I think you should be using string (C++ ones) rather than cstring

here is a page to it

http://www.cplusplus.com/reference/string/
Last edited on
also for the ouput why not just use for starting from the end, just kidding
I havent test this but
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithim> //for std::reverse
#include <string> /*better than cstring might come down to personal though but this is the standard*/

int main()
{
    std::string lol = "olleh";
    std::reverse(lol.begin(), lol.end());
    std::cout << lol; //should be hello
}
Last edited on
also for the ouput why not just use for starting from the end, just kidding

this can actually be done:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
    std::string myString = "helloWorld";
    for (auto itr = myString.rbegin(); itr!= myString.rend(); ++itr)
        std::cout << *itr ; std::cout << "\n";
}
Another easy way:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
  std::string org = "olleh";
  std::string reversed(org.rbegin(), org.rend());
  std::cout << "Reversed: " << reversed;
}
Using std::reverse_copy() with std::ostreambuf_iterator<char> …
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
    std::string myString = "helloWorld";
    std::reverse_copy(myString.begin(), myString.end(), std::ostreambuf_iterator<char>(std::cout));
}

Alternative version:
1
2
3
4
5
int main()
{
    std::string myString = "helloWorld";
    std::copy(myString.rbegin(), myString.rend(), std::ostreambuf_iterator<char>(std::cout));
}
Last edited on
Another option: std::experimental::ostream_joiner
http://en.cppreference.com/w/cpp/experimental/ostream_joiner
sorry to bang on about this but the nice thing here is that it allows delimiters b/w individual elements of the string that is not possible with the std::ostreambuf_iterator AFAIK.
There are also some interesting differences with std::ostream_iterator, viz:
Compared to std::ostream_iterator, ostream_joiner prints the delimiter sequence one fewer time, and is not templated on the type of the object to be printed.

But being experimental I suppose it's not a full-fledged member of the standard (yet):
1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <experimental/iterator>
#include <iostream>
#include <iterator>

int main()
{
    std::string i = "helloWorld";
    std::reverse_copy(std::begin(i),
              std::end(i),
              std::experimental::make_ostream_joiner(std::cout, " "));
}


hello
thank you all for your help
but my teacher he say today i need to print the in reverse order bu using (for loop) i will try it and see if i can make it work.
 i need to print the in reverse order bu using (for loop)

a relatively straightforward approach:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
    std::string myString = "helloWorld";
    for (std::string::size_type i = myString.size()-1; i != -1; --i)
    std::cout << myString[i]; std::cout << "\n";
}
thank you gunnerfunner
Topic archived. No new replies allowed.