Home > Mobile >  The answer outputs blanks
The answer outputs blanks

Time:10-26

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;
}

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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');

CodePudding user response:

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 "

  •  Tags:  
  • c
  • Related