View Single Post
Old 04-24-2005, 09:56 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
Here is one in C++, altho I think Valmont can come up with a more pretty solution.
Code:
#include <iostream>
#include <string>

const int LINE_SIZE = 20;
using namespace std;

int main()
{
  string correct_word;
  char temp_word[LINE_SIZE+1];
  int i, j;
  cout << "Please enter word to be reversed: ";
  cout.flush();
  cin >> correct_word;

  /* correct for the '\0' from the read string */
  j = correct_word.size() -1;
  for(i = 0; j >= 0; )
      temp_word[i++] = correct_word[j--];
  /* make sure reversed word isn't ending in garbadge */
  temp_word[i] = '\0';
  string reverse_word(temp_word);
  cout << "Read word was: " << correct_word << endl;
  cout << "Reversed word: " << reverse_word << endl;
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote