Reverse strings

This is a discussion on Reverse strings within the C Programming forums, part of the General Programming Boards category; Does anyone know how to make it so an output for a program would print out the string in reverse/backwards? ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    24

    Reverse strings

    Does anyone know how to make it so an output for a program would print out the string in reverse/backwards? I was just curious on how to do this. Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Start at the end of the string, and print out each character as you move to the beginning of the string.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Code:
    #include<stdio.h>
    #include<string.h>
    intmain(){
    charstr[50];
    char*rev;
     printf("Enter any string : ");
     scanf("%s",str);
     rev = strrev(str);
    
     printf("Reverse string is : %s",rev);
    
    return0;
    
    Last edited by Salem; 02-20-2013 at 10:47 PM. Reason: restoration

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Aside from the lack of whitespace in certain areas, your program breaks when certain kinds of input is received, and it depends on a function (strrev) that isn't declared in either stdio.h or string.h. I suggest learning what strings are, as well as how to manipulate them.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,308
    @return 0:
    Don't delete your post like that, it ruins the purpose of this forum, for people to learn. Now Barney McGrew's post makes no sense, since the code he's referencing is gone. Also, nobody else can learn anything from this thread now. Editing a post should only be used for small changes (e.g. typos) or small additions. Please put it back if possible.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Barney's posts never make any sense anyway - unless you want to learn how to program.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    The formatting is a mess because the code was not pasted at text, but instead in some pre-decorated horror from whatever IDE is being used.

    This is the first line.
    [ FONT=courier new][ COLOR=#7F0055][ FONT=Courier New]#include[ /FONT][ /COLOR][ COLOR=#2A00FF][ FONT=Courier New]<stdio.h>[ /FONT][ /COLOR][ FONT=Courier New][ /FONT][ /FONT]

    The board's code formatter then barfs, and well you can see the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  2. strings in reverse order
    By cakestler in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 04:32 PM
  3. Display Reverse Number, no strings or arrays
    By John Smith in forum C Programming
    Replies: 7
    Last Post: 12-08-2004, 04:29 AM
  4. reverse
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-26-2002, 04:13 PM
  5. how do we reverse ??
    By godfather in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2002, 07:48 PM

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