Thread: Reversing a String in C

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

    Reversing a String in C

    I need to take anything that is given at a prompt, text or numbers, and put it in a string. Then the program returns whatever was typed in exactly backwards. Example Hello World would come out dlroW olleH. Any help would be much appreicated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We do not do homework here. Put some effort into it. Post your code. (IE: Try it on your own.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    6
    I wasn't asking for someone to do it for me, I just wanted some suggestions. Here's what I have so far:

    #include <stdio.h>

    #define SIZE 1024


    int main()
    {
    char name[SIZE];
    printf("Enter input: ");
    gets(name); //The gets function reads 1 line from standard input



    return 0;

    }

    The only thing I can come up with is to use strlen() in some way. Thanks for any help

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, here's what you need to do:

    1) never ever use 'gets'. Use fgets instead. It is safer.
    2) Use a loop, starting at the last character in your name and counting backwards, displaying each letter in the name.

    To get the name length, use the strlen function.

    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Here is a fun way:
    But you're not allowed to have fun according to most teachers, it's very serious business.
    Then again, most teachers are idiots.

    Here is a more conventional way:
    Code:
    void reverse ( char *a )
    {
      int temp, x, y = strlen ( a ) - 1;
      for ( x = 0; x < y; x++, y-- )
        temp = a[x], a[x] = a[y], a[y] = temp;
    }
    [highly pedantic]
    >int main()
    When declaring main to take no parameters in C, it's acceptable to leave it empty, but better to explicitly say void:
    int main ( void )
    [/highly pedantic]

    >//The gets function reads 1 line from standard input
    Correct, now tell me what gets does to make sure that it doesn't read more than your array can hold.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe just use the strrev function, if you got it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Maybe just use the strrev function, if you got it.
    Or not, for three reasons:

    1) strrev is not a standard function, so he may not have it.
    2) This reeks of homework, in which case it must be done the hard way.
    3) I just like disagreeing with people for some reason.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM