Thread: question 1-9 The c programming language K&R

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    3

    question 1-9 The c programming language K&R

    Hi, I am relatively new to programming and reading through Kernigan and ritchies book. Everything was going fine untill i encountered this question that has struck me for a while. The question is :

    " write a program that copy it's input to it's output replacing each string of one or more blanks by a single blank "

    The output presented below prints -1 and as in the exercise i would like that more than one blank is produced as a blank. What am i doing wrong? (probably a lot)


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(VOID)
    {
    int c;
    int blanks = 0;
    while((c = getchar()) != EOF){
        if(c == ' ')
            putchar(c);
        if(c != ' ')
            putchar(blanks);
    }
    blanks = c;
    
    
    printf("%d,%d", c, blanks);

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It seems to me you misunderstood what the exercise is asking for. For example:
    Input:
    Code:
    This      is   a spaced-out                  sentence
    Output should be:
    Code:
    This is a spaced-out sentence
    Let's analyze what you do here:
    If the character is a space, you output a space
    If the character is anything but a space, you output the ASCII equivalent of number zero, which has no character representation.
    After you go through all the characters, you save in the variable "blanks" the only possible value that it can have, EOF, because only by EOF you exit the loop.
    In the end, you print the values of "c" and "blanks", both of which should be EOF(-1).

    Is that what you wanted to do?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    3
    No it's not what i wanted to do. Now that i understand a bit better (not completely) i have adjusted the code to this:

    Code:
    int main()
    {
    int c;
    int blanks = 0;
    printf("Enter a spaced sentence!\n");
    while((c = getchar()) !=EOF){
        if(c == ' ')
            putchar(' ');
        if(c != ' ')
            putchar(c);
    However i seem to miss the if statements that reduces blanks that are > 1 to 1. Therefore the written output is reproduced but not formatted. How to go about this task? I think i need to decrement but before that i would need a condition.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You already have a "blanks" variable defined, you can use it to keep track of how many spaces you've encountered so far. You should print a seen space only when that number is zero, you should increment that number when you see a space and make it zero when you see anything other than a space.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-18-2012, 05:13 PM
  2. Question addressing the c programming language.
    By errigour in forum General Discussions
    Replies: 9
    Last Post: 04-30-2012, 06:08 PM
  3. Replies: 2
    Last Post: 11-30-2011, 09:59 PM
  4. Replies: 5
    Last Post: 09-19-2011, 03:06 AM
  5. Replies: 8
    Last Post: 07-30-2008, 05:37 AM

Tags for this Thread