C program that prints its input one word per line




















However, when I run it, the program basically just outputs the same sentence. Is there another way I can do this? Many thanks. It seems you want something more like:. By the way, due to the way markdown formats text, the 'one word per line' thing was not clear, I had to fake-edit your post to see what exactly you meant. I think using a code tag would solve that. Edit 1: The fundamentals of the assignment The assignment requires you to extract letters from the input string to form a word.

There are many ways to do this. Some people start from the first letter in the sentence and search for the next character that is not a letter:. And they use the returned position to get a substring std::string::substr between the two positions. Another approach is to use a loop. If the present character is a letter, append to the word string. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.

Asked 8 years ago. Active 8 years ago. Every time we hit a blank space, tab, or newline, the status is changed to OUT. Please offer only suggestions based on what i'm learning in the book up to this point, "symbolic constants, if else, and loops only.

Overall it looks like you are in the right track, though I think you have overcomplicated things a bit with that custom state flag and constants. C99 gives you the bool type, which could be used instead.

Failing that, I would have settled with an integer. The defines seem overkill for such a tiny program. You can omit the return 0 at the end of main, it is implicit C99 standard.

Be advised that this is a compiler extension and is therefore non-portable. The standard C entry point function is called just main and it should take no arguments, unless you need to process the command line. Prefer exposing your include dependencies instead of relying on the auto-generated stdafx.

This is how I would have written it, using a simple bool to keep track of the state, and, as Loki suggested, using isspace to test the character always avoid reinventing the wheel, unless you really mean to :. But lets assume "white space separated". There is a function to detect white space isspace So you can simplify your test. I think it can be solved without maintaining the states..

Paras Singh Paras Singh 1 1 gold badge 3 3 silver badges 11 11 bronze badges. Arshpreet Soodan Arshpreet Soodan 9 4 4 bronze badges. You can try this one. Prateek Joshi Prateek Joshi 3, 3 3 gold badges 37 37 silver badges 48 48 bronze badges. Aditya kumar Aditya kumar 76 1 1 silver badge 10 10 bronze badges.

Aleem Isiaka Aleem Isiaka 1 1 silver badge 6 6 bronze badges. Uses state but only one call to getchar. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. In this C programming tutorial, we will learn how to read one user input string and how to print each word of that string in a new line. Our program will ask the user to enter a string, it will read that string and print each word of that string in a new line.

Web TypeScript React. Backend Node. Python tutorials. Java tutorials. C tutorials. Javascript tutorials.



0コメント

  • 1000 / 1000