K&R Exercise 1-9 (C)

后端 未结 30 1162
北荒
北荒 2021-01-31 19:46

\"Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.\"

I\'m assuming by thi

30条回答
  •  爱一瞬间的悲伤
    2021-01-31 20:27

    I am at the same point in the book. and my solution goes with making a count++ if blank is found and making the count back to zero if anything other than blank is found.

    For if statement I put another another check to check value of count (if zero) and then print.

    Though at this point of learning I shouldn't be concern about efficiency of two methods but which one is efficient a.) Accepted solution here with while inside while or b.) the one I suggested above.

    My code goes like below:

    #include 
    
    
    main()
    {
        int count=0,c;
        for( ; (c=getchar())!=EOF; )
        {
            if(c==' ')
            {
                if(count==0)
                {
                    putchar(c);
                    count++;
                }
            }
            if(c!=' ')
            {
                putchar(c);
                count=0;
            }
    
    
        }
    }
    

提交回复
热议问题