user2186062
user2186062

Reputation: 41

The C Programming Language K&R exercise 1- 9

I'm new on here and relatively new to programming logic in general. In an effort to develop my skill I've begun reading this fine piece of literature. I really feel that I am grasping the concepts well but this exercise seems to have caught me off guard. I can produce the program but some of the examples I've seen seem to introduce some concepts not yet covered by the book like the examples here. inspace seems to be serving a function that is more than just a variable created by the programmer.

#include <stdio.h>

int main(void)
{
    int c;
    int inspace;

    inspace = 0;
    while((c = getchar()) != EOF)
    {
        if(c == ' ')
        {
            if(inspace == 0)
            {
                inspace = 1;
                putchar(c);
            }
        }

        /* We haven't met 'else' yet, so we have to be a little clumsy */
        if(c != ' ')
        {
            inspace = 0;
            putchar(c);
        }
    }    
    return 0;
}

In the next example, pc seems to be doing something in regards to counting spaces but I'm not sure what.

I managed to create a program that completes this task but it was using only the variable c that I created, thus I understand its purpose.

Upvotes: 4

Views: 2101

Answers (4)

Singh
Singh

Reputation: 229

I was also having the same problem but finally got a program that works.

    #include<stdio.h>

/* copy input to its output, replacing each
   string of one or more blanks by a single blank */

int main()
{
    int c, nspace=0;
    while((c=getchar()) != EOF){
        if(c==' ') ++nspace;
        else{
            if(nspace >= 1){
                printf(" ");
                putchar(c);
                nspace=0;
            }
            else
                putchar(c);
        }
    } 
            
}

Upvotes: 0

kostas-p
kostas-p

Reputation: 21

Took me a while but this is the answer I think.

#include <stdio.h>

main()
{
    int c, blank;
    blank = 0;

    while ((c=getchar()) != EOF){
        if (c == ' '){
            if (blank == 0){
                printf("%c", c);
                blank = 1;
            }

        }
        if (c != ' '){
            if (blank == 1){
                blank = 0;
            }
            printf("%c", c);
        }


    }
}

Upvotes: 2

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58291

The objective of this code is copy text and if there is more then one spaces ' ' consecutive print only one space.

Variable inspace is used to keep track of whether last time printed char was scape or non-space.
if inspace is zero means a char was printed that was not space. and
if inspace is one means a last time space was printed.

So if inspace is zero next time scape can be printed on reading a scape, and if inspace is one then next consecutive scape found so not to print space.

See C is current char read. (read comments)

  if(c == ' ')  // currently space read
    {
      if(inspace == 0) // last time non-space printed, So space can be print
      {
        inspace = 1;   // printing space so switch inspace 1
        putchar(c);   // print space
      }
    }

Next if

if(c != ' ') // A char not space read, its to to print unconditionally  
{
  inspace = 0;  // remember that non-scape print
  putchar(c);
}

Upvotes: 2

mah
mah

Reputation: 39847

inspace is essentially a variable to indicate you are or are not in the "just seen a space" state. You enter this state after seeing a space, and you exit this state when you see a non-space. You print your input only if you're not in the inspace state, thus you do not print multiple adjacent spaces.

I managed to create a program that completes this task but it was using only the variable c that I created, thus I understand its purpose. In your program, if the input is "hello world", is that its exact output? The program you posted will output "hello world" (compressing the multiple spaces between the words down to one).

Upvotes: 1

Related Questions