Muhammad Mahgoub
Muhammad Mahgoub

Reputation: 23

Exercise 1-12. Write a program that prints its input one word per line

Going through the K&R's The C Programming Language and I wrote a code for Exercise 1-12 that for all intents and purposes seems to work. However, I became curious how it could be adjusted for more lengthy input that would span multiple lines. The current program I wrote terminates the input as soon as I hit enter. Is there a way to adjust the program so input is only terminated when I desire and not by the newline character? Here's the program I used.

#include <stdio.h>

main()
{
    int c;

    while ((c = getchar()) != EOF)
    {
        if ( c == ' ' || c == '\n' || c == '\t' || c == '-')
            putchar('\n');
        else
            putchar(c);
    }
}

Thanks in advance.

Upvotes: 0

Views: 2854

Answers (5)

JOSEPH NGONGO
JOSEPH NGONGO

Reputation: 1

This code below worked for me

int main()
{
   
    int ch;
    while ((ch = getchar()) != EOF)
    {
        if (ch == ' ' || ch == '\t')
        {
            putchar('\n');
        }
        else
            putchar(ch);
    }
}```

Upvotes: 0

fraxl
fraxl

Reputation: 1

#include <stdio.h>
#define NONBLANK '-'
int main(){
    int c,lastc;
    lastc = NONBLANK;
    while ((c = getchar()) != EOF){
        if (c == ' ')
            if (lastc == ' ' || lastc == NONBLANK)
                ;
            else
                putchar('\n');
        else 
            putchar(c);
        lastc = c;
    }
}

I think this will do the job but the weakness of these code is that if you put ' ' once or many time at the start of the line it will skip the first line.

Upvotes: 0

Wallie
Wallie

Reputation: 1

I just used space and tab but you can add on anything else to the word separation. This code makes sure extra spaces or tabs don't make any blank lines.

int c;
while ((c = getchar()) != EOF){
        if (c == ' ' || c == '\t'){
            putchar('\n');
            while ((c = getchar()) == ' ' || c == '\t');  
        }
        putchar(c);

Upvotes: 0

maxdev
maxdev

Reputation: 2566

Your code is fine - the problem is that a terminal by default buffers all content that you type in until you hit Enter. Then, the content you typed is actually written into the processes stdin.

To do what you want, you could simply read in lines until the user for example types "print". Then you can go over each character of your array and do the same as you already did.

This pseudo-C-code will illustrate how you could solve it:

for(;;) {
    // create a buffer that will hold the entered lines
    char* buffer = ...;

    // read multiple lines from the terminal until user types "print"
    for(;;) {
        char* line = readLine();

        if(strcmp(line, "print") == 0) {
            break;
        } else {
            // add the entered line to the buffer (null-terminating)
            addLineToBuffer(buffer, line);
        }
    }

    // perform your output loop for the characters in the buffer
    char* pos = buffer;
    while(*pos) {
        if (*pos == ' ' || *pos == '\n' || *pos == '\t' || *pos == '-') {
            putchar('\n');
        } else {
            putchar(*pos);
        }
        ++pos;
    }
}

Upvotes: 1

P.P
P.P

Reputation: 121387

The program would receive input only if you press the ENTER key as that's how a typical terminal operates in canonical mode. So what you ask to do is not straight forward. However, you could use a file to input to your program1:

$./a.out < inputfile

or type entire text using a here-document:

$./a.out << _TEXT
type all the 
text you want here
and terminate 
it with CTRL+D 
which sends EOF to your program
or type _TEXT

(1) I am assuming a unix style shell. Windows powershell provides same facilities too.

Upvotes: 3

Related Questions