he printing a string in reverse order

This is a discussion on he printing a string in reverse order within the C Programming forums, part of the General Programming Boards category; ok. this what i want to do, have the user enter a sentence, then separated by words, and finally reverse ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    help printing a string in reverse order

    ok. this what i want to do, have the user enter a sentence, then separated by words, and finally reverse the words in reverse in one line.

    the final reverse sentence prints like this:
    red
    is sky the

    I want it to print in one line:
    red is sky the

    Code:
     
    Last edited by newbc; 02-11-2011 at 10:28 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is there a question in there someplace?

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	printf("% ", temp[i]);
    perhaps %s?


    int size=(unsigned long) strlen (string);

    What's that non-sense?
    string is not initalized yet.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by Bayint Naung View Post
    Code:
    	printf("% ", temp[i]);
    perhaps %s?


    int size=(unsigned long) strlen (string);

    What's that non-sense?
    string is not initalized yet.
    Sorry I deleted %s by mistake, but even when I added, it still prints the first word on one line and the rest on the second line.


    int size=(unsigned long) strlen (string);

    well this is for a second part or the program so I can know the length, but im using gcc and since Im not using it right now it just ignores it.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by newbc View Post
    ok. this what i want to do, have the user enter a sentence, then separated by words, and finally reverse the words in reverse in one line.

    the final reverse sentence prints like this:
    red
    is sky the

    I want it to print in one line:
    red is sky the
    Your using fgets(), red is the last word and fgets() includes '\n' in the string. You need to strip off the new line from the string to avoid this.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by newbc View Post
    Yeah i figured it had to do something with this, but I have no i idea what to do know.
    On your last token .... token[i][strlen(token[i]) - 1] = 0;

    Note the square brackets are deliberate...

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    After you read the line with fgets().
    You find the newline, and if exists write '\0' to that location.
    eg.
    Code:
      p= strchr(buf,'\n');
      if(p) *p = '\0';

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    After you read the line with fgets().
    You find the newline, and if exists write '\0' to that location.
    eg.
    Code:
      p= strchr(buf,'\n');
      if(p) *p = '\0';
    Yep... easier than my way. Thanks.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    will this work?

    Code:
     p= strchr(buf,'\n');
    if(*p== '\n'){
      
     *p= '\0';
    
    }
    will I declare p as char *p;?

    thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. [Help] About reverse string
    By kingofdcp in forum C Programming
    Replies: 3
    Last Post: 06-06-2009, 02:53 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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