Hi,
I've written a small program for my programming class, that uses pointers to reverse a char array. I was wondering if there is anything that I should do differently? Am I doing this correctly? Is there a more efficient way to accomplish this? Thanks!
My small program:
int main ( )
{
char buffer[80];
PrintHeader();
cout << "\nString reversal program";
cout << "\nType in a short string of words.";
cout << "\nI will reverse them.";
cout << "\n:";
cin.getline(buffer, 79);
cout << "\nYou typed " << buffer;
reverse (buffer);
cout << "\nReversed: " << buffer;
cout << endl;
system("PAUSE");
return 0;
}
void reverse(char* string)
{
char* pStart, *pEnd;
int length;
char temp;
length = strlen(string);
pStart = string;
pEnd = &string[length - 1];
while(pStart < pEnd)
{
temp = *pStart;
*pStart = *pEnd;
*pEnd = temp;
pStart++;
pEnd--;
}
}