Randy Fitzgerald
2011-04-21 13:09:19 UTC
should use two pointers, 'front' and 'rear'. The 'front' pointer should initially reference the first
character in the string, and the 'rear' pointer should initially reference the last character in the
string. Reverse the string by swapping the characters referenced by 'front' and 'rear', then increment
front to point to the preceding character, and so on, until the entire string is reversed. Write a
main program to test your function on various strings of both even and odd length......
SOURCE CODE:
#include
#include
using namespace std;
const int MAXIMUM_LENGTH = 100;
void swap(char a[], int size);
int main()
{
char userEntry[MAXIMUM_LENGTH];
cout << "Enter a string of characters and I will reverse them: \n" <
char *words;
words = new char[MAXIMUM_LENGTH];
int length = strlen(userEntry);
cout << "\nThe length of the string is " << length << " characters.\n";
swap(userEntry, length);
return 0;
}
void swap(char a[], int length)
{
int i = 0;
char *rear;
char *front;
rear = &a[i];
*rear = a[i];
front = &a[i];
*front = a[i];
for(i = 0; i < length; i++)
{
*rear = a[length-1];
}
cout << *rear; //JUST A DRIVER
}
Any hints/suggestions!? I think it's just the fact of a poor algorithm.