Thread: How can I print a string backwards?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    How can I print a string backwards?

    Hello,

    I have declared and input a string in a array.
    How can I print this "string" backwards?

    Here is my code:

    Code:
    #include<stdio.h>
    
    int main()
    {
        char a[1][20];
    
        int i;
    
        scanf( "%s", a[0] );
    
        printf( "\n\n%s\n\n", a[0] );
    }
    Last edited by ahmedbatty; 12-07-2011 at 09:27 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... I know this may seem a little harsh, but before I show you, I want you to stop and think about how much thought you gave this before you posted here asking for answers. This is a very basic and simple thing...

    Code:
    int x;
    
    for (x = strlen(string); x >= 0 ; x--)
      printf("%c",string[x]);
    Really, my friend... you should be able to work these things out on your own.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    sorry

    i did it:

    Code:
    #include<stdio.h>
    
    int main()
    {
        char a[1][20];
    
        int i;
    
        scanf( "%s", a[0] );
    
        printf( "\n\n%s\n\n", a[0] );
    
        for( i = 3; i >= 0; i-- )
        {
            printf( "%c", a[0][i]);
        }
    
        printf( "\n\n" );
    }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Just find the length of the string using strlen() then print each char backwards starting at the length of the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-11-2010, 12:21 AM
  2. Print backwards? Can someone please help me?
    By matthayzon89 in forum C Programming
    Replies: 11
    Last Post: 04-23-2010, 07:30 AM
  3. Searching backwards in a string
    By Ducky in forum C++ Programming
    Replies: 11
    Last Post: 03-09-2008, 01:54 AM
  4. New programmer: how to print a string
    By 9988776655 in forum C Programming
    Replies: 1
    Last Post: 12-27-2007, 07:32 PM
  5. how to print a string backwards
    By Unregistered in forum C# Programming
    Replies: 2
    Last Post: 01-27-2002, 01:04 PM