in the printf the program remove all the strings after the space why???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ehab mohamed
    New Member
    • May 2010
    • 18

    in the printf the program remove all the strings after the space why???

    Code:
    #include<stdio.h>
    void num(char arr[100])
    {
    int i=0, count=1;
    while (arr[i]!='\0')
    {
    if (arr[i]==' ')
    {count++;}
    i++;
    }
    printf("%d",count);
    }
    int main()
    {
    
    char data [100];
    
    scanf("%s",&data);
    printf("%s",data);
    num(data);
    return 0;
    }
    try to inter programming language
    in the printf in the main it will printf programming only and remove the rest
    Last edited by Dormilich; Dec 7 '10, 11:58 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    No that is not printf getting it wrong, that is scanf behaving exactly as specified.

    scanf uses any white space as a delimiter so if you ask scanf for a string and enter "programmin g language" it will use the space as a delimiter and return "programmin g" which printf then correctly outputs.

    If you want to get a whole line including spaces using the end of line as a delimiter then use the function fgets.

    Comment

    • ehab mohamed
      New Member
      • May 2010
      • 18

      #3
      how can u give me an example

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Perhaps you could try typing "fgets" into your browser search engine?

        Comment

        • ehab mohamed
          New Member
          • May 2010
          • 18

          #5
          okai thanks

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Better than typing 'fgets' into the browser ...
            1. Look for "bytes > c/c++ > c/c++ questions" in small print near the top of this browser page (just above the thread title).
            2. Click on "c/c++ questions".
            3. Then click on the first question: "PLEASE READ FIRST: Useful Links and Posting Guidelines"
            4. Scroll down to the second message in this thread. It contains Important Links.
            5. Click on either of the "C and C++ library reference" links.
            6. Now search for fgets.

            Comment

            • ehab mohamed
              New Member
              • May 2010
              • 18

              #7
              when i replace the space between the words by tab it didn't work in a right way
              any help in this !!

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                There is no reason why it would work any differently, tab is also a white space character.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Refer to reply #2. Use fgets. Don't use scanf.

                  Comment

                  • mysticwater
                    New Member
                    • Aug 2010
                    • 14

                    #10
                    Options to get rid of current problem:
                    gets() function instead of scanf [although this is often frowned upon]
                    fgets() function instead of scanf

                    or continue using scanf but clear the buffer with a while loop.
                    Google: clean buffer after scanf for more details on this.

                    Comment

                    Working...