Thread: Printing String in reverse order

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Unhappy Printing String in reverse order

    I want to print all the characters (including spacing and special characters) in a sring in reverse order. But the program fail to do it. :confused


    Can anyone tell me what's wrong in my program?? am i use the wrong sequence?
    Thank you so much.

    # include <stdio.h>

    char string[80]; //global variable

    void main()
    {
    char s;

    int i=0, num = 0, item=0;

    printf ("String : ");
    scanf ("%[^\n]", string);

    while (s = (string[i]) != '\0')
    {
    num++;
    i++;
    };

    printf ("num = %d\n", num);

    for (i= num-1; string[i] <= 0;i--)
    {
    string[item] = string[i];
    item++;
    }

    printf ("\nString Reverse = %s \n", string);

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    #include <iostream.h>
    #include <string.h>

    void main()
    {

    char string[]= "The Hello World sentence!";

    cout << "Before: "<<string<<endl;

    _strrev(string);

    cout << "After: "<<string<<endl;


    }

    so, in few words- use _strrev(string_name) from the string.h library
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Ahhhh! loobian..... C++ in the C forum again .

    ok ling.............

    declare two strings say
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
    char my_str[11] = "new string";
    char new_str[11];
    int i, j;
    
    for(i = 0, j = strlen(my_str) - 1; j >= 0; i++, j--)
          new_str[ i ] = my_str[ j ];
    new_str[i]= '\0'; /*add null char*/
    
    printf("%s reversed is %s", my_str, new_str);
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Smile Thanks bigtamscot!!

    Thanks bigtamscot, now I know how to do printing the string in reverse order without using strrev();

    you solved the problem that i tried for one whole night..(how stupid I am..)

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Question

    Hi, Salem, what are the different between using void main() and main() (return 0).....

    will it cause any problem???? Thank you.

  6. #6
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Try it. should work.

    #include "stdio.h"

    int main() // int main, void main, blah blah blah all the same to me
    {
    // Declare a char type var.
    char string[1024];

    // Get input from user
    printf("\nType something: ");
    gets(string);

    // Show user what was typed originally
    printf("\n\nYou typed, %s",string);

    // My little way of getting the number of characters in a string
    int i = 0;
    while(string[i] != 0){ // 0 or NULL, whatever
    i++;
    }
    // i, now equals how many chars were into 'string'

    // Reverse string
    printf("\n-Reversed, ");
    int idx;
    for(idx = 0;idx < i;){
    i--; // I place this here instead of in the for loop,
    // because the for loop will subtract 1 from i
    // before 'string' can even begin to print, thus
    // the last char will not be printed.
    printf("%c", string[i]);
    }
    printf("\n\n");

    return 0;
    }

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    >ling I want to print all the characters (including spacing and special >characters) in a sring in reverse order. But the program fail to do it. >:confused

    another approach using recursion

    #include <stdio.h>

    void printrev(char *s)
    {
    if(*s)
    {
    printrev(s+1);
    putchar(*s);
    }
    }

    int main(void)
    {
    printrev("a man a plan a canal panama");
    putchar('\n');
    printrev("rest");
    putchar('\n');
    printrev("pest");
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Smile

    Thanks knave and pinko_liberal. ^_^

    I know how to doing the reverse priting in many ways now.




  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > what are the different between using void main() and main() (return 0)
    void main is wrong - plain and simple
    int main is right.

    > will it cause any problem?
    There is always that possibility when you do something wrong.
    The simple excuse of 'it seems to work' doesn't wash in the long run, because sooner or later, most things you do wrong will bite (and you've got some unlearning to do).
    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.

  10. #10
    Unregistered
    Guest
    You guys are doing this the hard way.
    Code:
    #include <stdio.h>
    
    void printStringRev( char *s )
    {
       if( s && *s )
       {
          printStringRev( s+1 );
          fputc( *s, stdout );
       }
    }
    
    int main( void )
    {
       char buf[1024]={0};
       puts("Enter some text:");
       fgets( buf, 1024, stdin );
       printStringRev( buf );
       return 0;
    }
    Quzah.

  11. #11
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Unregistered
    You guys are doing this the hard way.
    Quzah.
    ehhh ,why ? Why I think my program and yours are exactly the same except that your program tests for s being NULL also .

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Smile

    Thank you salem.

  13. #13
    Unregistered
    Guest
    Ya, Pinko, I didn't see yours when I wrote mine. It's possible we started them at similar times. (Or perhaps I just missed it.)

    Quzah.

  14. #14
    0x01
    Join Date
    Sep 2001
    Posts
    88
    No problem ling.

  15. #15
    Unregistered
    Guest

    Thumbs up Re: Printing String in reverse order

    /* Hi my name is Rahat and i think i am able to solve your problem. EnJoY......*/

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    int main(void)
    {
    clrscr();
    char *string;
    int str_len;

    printf ("Enter a string: ");
    gets(string);

    str_len=strlen(string);
    printf ("Here is the result....:\n");
    for(int i=str_len;i>=0;i--)
    printf("%c",string[i]);

    getch();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] About reverse string
    By kingofdcp in forum C Programming
    Replies: 3
    Last Post: 06-06-2009, 02:53 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Printing name in reverse order and in caps
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2003, 07:02 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM