Morteza R
Morteza R

Reputation: 2319

printing a word per line

I need to write a program that prints its input one word per line. Here's what I got so far:

#include <stdio.h>
main(){
    int c;
    while ((c = getchar()) != EOF){
        if (c != ' ' || c!='\n' || c!='\t')
            printf("%c", c);
        else 
            printf("\n");
    }
}

The logic is pretty simple. I check to see if the input is not a newline, tab or space, and in that case it prints it, otherwise prints a newline.

When I run it, I get results like this:

input-->  This is
output--> This is

It prints the whole thing. What goes wrong here?

Upvotes: 0

Views: 2691

Answers (5)

Paras Singh
Paras Singh

Reputation: 403

I think the simple solution would be like

#include <stdio.h>

int main(void) {
    // your code goes here
    int c;
    while((c=getchar())!=EOF)
    {
        if(c==' ' || c=='\t' || c=='\b')
        {
            printf("\n");
            while(c==' ' || c=='\t' || c=='\b')
            c=getchar();
        }
        if(c!=EOF)
        putchar(c);
    }
    return 0;
}

Upvotes: 0

Vino
Vino

Reputation: 922

instead of using printf try putchar, also as per above comments, you should use && instead of ||.

here is my code-

#include<stdio.h>

main()
{
    int c, nw;                  /* nw for word & c for character*/

    while ( ( c = getchar() ) != EOF ){

    if ( c != ' ' && c != '\n' && c != '\t')
    nw = c;
    else {
        nw = '\n';
    }

    putchar (nw);

    }

}

this code will give you the desired output

Upvotes: 1

user3629249
user3629249

Reputation: 16540

suggest the code implement a state machine, 
where there are two states, in-a-word and not-in-a-word.  
Also, there are numerous other characters that could be read 
(I.E. ',' '.' '?' etc) that need to be check for. 

the general logic:

state = not-in-a-word
output '\n'

get first char
loop until eof
    if char is in range a...z or in range A...Z
    then 
        output char
        state = in-a-word
    else if state == in-a-word
    then
         output '\n'
         state = not-in-a-word
    else
         do nothing
    end if
    get next char
end loop

output '\n'

Upvotes: 0

you can use if you want the strtok function in string.h library which can cut the input into many words by providing a delimiter.

Here is a perfect code commented which can fit to your needs

#include <stdio.h> 
#include <string.h> 

int main(int argc, char *argv[])
{
    char line[1000]=""; // the line that you will enter in the input
    printf("Input the line:\n>>");
    scanf("%[^\n]",line); // read the line till the you hit enter button
    char *p=strtok(line," !#$%&'()*+,-./'"); // cut the line into words 
    // delimiter here are punctuation characters  (blank)!#$%&'()*+,-./'

    printf("\nThese are the words written in the line :\n");
    printf("----------------------------------------\n");
    while (p!=NULL) // a loop to extract the words one by one 
    {
        printf("%s\n",p); // print each word
        p=strtok(NULL," !#$%&'()*+,-./'"); // repeat till p is null 
    }

    return 0;
}

If we execute the code above we will get

Input the line:
>>hello every body how are you !

These are the words written in the line :
----------------------------------------
hello
every
body
how
are
you

Upvotes: 0

MuertoExcobito
MuertoExcobito

Reputation: 10039

if (c != ' ' || c!='\n' || c!='\t') This will never be false.

Perhaps you meant: if (c != ' ' && c!='\n' && c!='\t')

Upvotes: 7

Related Questions