0

Program task -

Enter a string, display it word for word on the screen.

The problem is that if you type a lot of spaces between words, they will show up when you check. How can this be fixed?

#include <stdio.h>


int main()
{

int inw = 0, i = 0, count = 0;
char s[10000];
printf("Print string (max 10000 sb):\n");
gets(s);
while (s[i] != '\0') {
    if (s[i] != ' ' && s[i] != '\t') {
        putchar(s[i]);
    }
    else if (s[i] == ' ') {
        printf("\n");
    }
    i++;
}



return 0;
}
  • @chux-ReinstateMonica Not sure that helps. It will still print a newline for every space. Which is what the OP doesn't want I think. OP wants a single newline even if there is more than one space between words. – kaylum Oct 25 '21 at 21:38
  • 1
    [never use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – yano Oct 25 '21 at 21:39
  • OK - unclear on the "they" in "they will show up". – chux - Reinstate Monica Oct 25 '21 at 21:39
  • @chux-ReinstateMonica nope, still a lot of blanks :c – KoraOnEarth Oct 25 '21 at 21:41
  • @chux-ReinstateMonica sry, I am not native eng speaker and my english is not very good. so sometimes I had to use google translate xd. – KoraOnEarth Oct 25 '21 at 21:43
  • @chux-ReinstateMonica problem is that in console when you are checking your code and testing it, there is a lot of blanks. I can send a screenshot of this trouble – KoraOnEarth Oct 25 '21 at 21:45
  • 1
    @KoraOnEarth Are you able to write down the logic and steps in words first? For example, use a flag that is set to true after a newline is printed and set to false only when a non-space is found. Then only print a newline when there is a space in the input and the flag is false. – kaylum Oct 25 '21 at 21:46
  • https://prnt.sc/1xbn170 – KoraOnEarth Oct 25 '21 at 21:46
  • Add a flag indicating the last printed character was `'\n'`, and do not print another one if it is set. – Eugene Sh. Oct 25 '21 at 21:46
  • Welcome to Stack Overflow. For future reference, please show your output by copying and pasting it and formatting it like code. It seems that what you mean is that a new *blank line* is output for each space in the input. Well, try thinking about what the code means. What part of your code is printing the blank lines? What is the rule, currently, that it uses to decide to do that? Can you think of a way to modify that rule to give you the result you want? How would you solve the problem (create the output) if you had to do it all by hand? – Karl Knechtel Oct 25 '21 at 21:54
  • BTW for "(max 10000 sb)" and `gets()`, use `char s[10000+1];`. With `fgets()`, use `char s[10000+ 2];` – chux - Reinstate Monica Oct 25 '21 at 21:54

4 Answers4

0

Ugly, but this gets the job done. Just need a flag to keep track of whether or not you just printed a new line. Also cleaned up unused variables and changed to using fgets

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int i = 0;
    char s[10000];
    bool justPrintedNewline = false;
    printf("Print string (max 10000 sb):\n");
    fgets(s, sizeof s, stdin);
    while (s[i] != '\0') {
        if (s[i] != ' ' && s[i] != '\t') {
            putchar(s[i]);
            justPrintedNewline = false;
        }
        else if (s[i] == ' ' && justPrintedNewline == false) {
            printf("\n");
            justPrintedNewline = true;
        }
        i++;
    }

    return 0;
}

Demo

yano
  • 4,827
  • 2
  • 23
  • 35
0

You did a great job in the algorithm just fix a little thing. You can create a flag and after space you increase the flag to 1. Then you will know you will print just one space. After printing " " check for a char that isn't " " for update the flag to 0. When the flag is 1 DONT print anything just wait for another valid char. Take care, Ori

0

Only print a line-feeed when starting a word and after all is done.

Change code to:

If a space
-- print a '\n' when the prior character is a non-white-space.
Else
-- if (prior character is white-space) print a '\n'
-- print it

char prior = 'a';
while (s[i]) {
  char ch = s[i];
  if (ch != ' ' && ch != '\t') {
    if (prior == ' ' || prior == '\t') {
      putchar('\n');
    }
    putchar(ch);
  }
  prior = ch;
  i++;
}
putchar('\n');
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

There is a bit of a trick to it: use a second, inside loop to skip past spaces and another to print words. The outer loop should only terminate if you have reached the end of the string.

while (s[i] != '\0')
{
  // skip all spaces
  while ((s[i] != '\0') && isspace( s[i] )) ++i;

  // print the word
  while ((s[i] != '\0') && !isspace( s[i] ))
  {
    putchar( s[i] );
  }

  // print the newline after a word
  putchar( '\n' );
}

By the way, gets() is a really, really dangerous function. It should never have been included in the language. You are OK to use it for a homework, but in reality you should use fgets().

char s[1000];
fgets( s, sizeof(s), stdin );

The fgets() function is a bit more fiddly to use than gets(), but the above snippet will work for you.

Your other option for solving this homework is to use scanf() to read a word at a time from user input, and print it each time through the loop. I’ll leave that to you to look up. Don’t forget to specify your max string length in your format specifier. For example, a 100 char array would be a maximum 99-character string, so you would use "%99s" as your format specifier.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39