string in reverse

This is a discussion on string in reverse within the C Programming forums, part of the General Programming Boards category; I need to write a program that reads one line from the keyboard as a string and then displays the ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    string in reverse

    I need to write a program that reads one line from the keyboard as a string and then displays the string in reverse print.


    The reading a line from the keyboard as a string I understand, and I can make it display the string...but the reverse is giving me fits. I either get a jumble or an error....any help on how to do this would be appreciated. Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I really like helping people that don't even read further down on the same freeking page!!!

    Here is a fun way:
    Code:
    void r( void )
    {
    	int c = fgetc(stdin);
    	if( c != '\n' ) r( );
    	printf("%c", c );
    }
    
    int main ( void )
    {
    	printf("Enter a string: ");
    	r( );
    	return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    heres another function that works (sorry I dont like function recursion, only reason I'm suggesting this one):
    Code:
    void stringreverse(char tempstr[])
    {
      char length;
      char counter;
      char tempchar;
    
      length = stringlen(tempstr)-1;
      for(counter=0;counter<(length-counter);counter++)
      {
        tempchar = tempstr[counter];
        tempstr[counter] = tempstr[length-counter];
        tempstr[length-counter] = tempchar;
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21