0

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.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 4
    The program only terminates if you close `stdin` (receiving `EOF`), which would be ctrl-D for POSIX systems and - IIRC - ctrl-Z for Windows. It does not terminate by newline or `CR`. So what is your problem actually? – too honest for this site Jul 03 '15 at 21:43
  • 2
    @Olaf To me, this *adjust the program so input is only terminated when I desire and not by the newline character?* is clear enough and describes the problem sufficiently. – P.P Jul 03 '15 at 22:01
  • @BlueMoon: Yes, but it already _does_ terminate input only by `EOF`! Except you are refering to the computer reading user's thoughts. – too honest for this site Jul 03 '15 at 22:05
  • @Olaf To clarify, I was using the Developer Command Prompt in the Visual Studio Tools to program and anytime I hit enter to start a new line of input the program would simply show the output, effectively prematurely ending my input. Anyway, I get it now thanks for taking the time to help me out. – Muhammad Mahgoub Jul 04 '15 at 05:36
  • Well, so much for thought-reading;-) – too honest for this site Jul 04 '15 at 13:29

5 Answers5

3

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.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

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;
    }
}
maxdev
  • 2,491
  • 1
  • 25
  • 50
0

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);
Wallie
  • 1
0
#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.

James Risner
  • 5,451
  • 11
  • 25
  • 47
fraxl
  • 1
  • 2
0

This code below worked for me

int main()
{
   
    int ch;
    while ((ch = getchar()) != EOF)
    {
        if (ch == ' ' || ch == '\t')
        {
            putchar('\n');
        }
        else
            putchar(ch);
    }
}```
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 03:33