Passing Values Question...
Hi Everyone! :)
This is my question...
The following program segmant asks the user for his or her first name in main(). Write a function that reverses the name in the array so that main() can print the reversed name. To make it easy, assume that the user name is always five characters long ("Jerry" or "Peter", for example).
Code:
#include <iostream.h> // I know it's the old method, but that's what's the book writes
rev(char name[6]);
main()
{
char name[6];
cout << "What is your first name (5 letters, please)? ";
cin >> name;
rev(name);
cout << "Your name spelled backwards is " << name << "\n";
return 0;
}
//from here is what I added
rev(char name[6])
{
name[0] = name[4];
name[1] = name[3];
name[3] = name[1];
name[4] = name[0];
return 0;
}
Is there a way to write it with a loop? My way doesn't work--the last character doesn't change :confused:
Thanks all.
Tinker.