View Single Post
Old 04-24-2005, 10:28 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
First of all strcpy() copies a string, not a char, what you do for your readin routine I have no idear of, since all it seems to be is a while loop that does absolutely nothing.

Something like this will do the trick:
Code:
#include <stdio.h>
#include <string.h>
#define LINE_SIZE 20

int main()
{
  char correct_word[LINE_SIZE +1], reverse_word[LINE_SIZE+1];
  int i, j;
  printf("Please enter word to be reversed: ");
  fflush(stdout);
  if(!fgets(correct_word, LINE_SIZE, stdin))
    {
      printf("Error reading word from stdin\n");
      return -1;
    }
  /* correct for teh '\n' and '\0' from the read string */
  j = strlen(correct_word) -2;
  for(i = 0; j >= 0; )
    reverse_word[i++] = correct_word[j--];
  /* make sure reversed word isn't ending in garbadge */
  reverse_word[i] = '\0';
  printf("Read word was: %s", correct_word);
  printf("Reversed word: %s\n", reverse_word);
  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