Thread: Reversed String don't like spaces

  1. #1
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31

    Reversed String don't like spaces

    IS it possible to use the rand function within a range, say 0 and 100?










    Code:
    #include<stdio.h>
    #include<string.h>
    #define N 30
    
    int main(void){
        char str[N];
        int i,k;
        scanf("%s",&str);
        k=strlen(str);
        for(i=k;i>=0;i--){
                         printf("%c",str[i]);
                         }
        system("pause");
    }
    Hello, dear community.

    So the problem I was proposed was to put together a code that would reverse a string this way:

    ABC DEF ---> FED CBA

    And the code above works fine until it reads a space character and it won't print anything past that point.

    Has anyone any idea why?

    Appreciate it.
    Last edited by KAUFMANN; 03-30-2011 at 05:45 AM. Reason: Didn't want to open a new thread for another simple question.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     scanf("%s",&str);
    %s stops on the first whitespace it encounters. You need a different format specifier, or to read your string differently, like say fgets.


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

  3. #3
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Quote Originally Posted by quzah View Post
    Code:
     scanf("%s",&str);
    %s stops on the first whitespace it encounters. You need a different format specifier, or to read your string differently, like say fgets.


    Quzah.
    Oh I feel silly now. Thank you for your help, fella

    Now, IS it possible to use the rand function within a range, say 0 and 100?
    Last edited by KAUFMANN; 03-30-2011 at 05:46 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Your loop is wrong. It needs to be
    Code:
    for(i=k-1;i>=0;i--){
    Yup rand() can go from 0 to 100. Just code rand() % 101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Replies: 3
    Last Post: 04-06-2005, 11:04 AM