Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3274512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:02:48+00:00 2026-05-17T19:02:48+00:00

Here was my original code: #include <stdio.h> #define IN 1 // inside a word

  • 0

Here was my original code:

#include <stdio.h>

#define IN  1   // inside a word
#define OUT 0   // outside a word

// program to print input one word per line

int main(void)
{
  int c, state;

  state = OUT;
  while ((c = getchar()) != EOF) {
    if (c == ' ' || c == '\n' || c == '\t') {
      state = OUT;
      printf("\n");
    }
    else if (state == OUT) {
      state = IN;
    }
    if (state == IN) {
      putchar(c);
    }
  }
  return 0;
}

But the problem was if there were multiple blanks (spaces) or multiple tabs next to each other a newline would be printed for both. So I used a variable (last) to keep track of where I was:

#include <stdio.h>

#define IN  1   // inside a word
#define OUT 0   // outside a word

// program to print input one word per line, corrected bug if there was
// more than one space between words to only print one \n

int main(void)
{
  int c, last, state;

  last = EOF;
  state = OUT;
  while ((c = getchar()) != EOF) {
    if (c == ' ' || c == '\n' || c == '\t') {
      if (last != c) {
        state = OUT;
        printf("\n");
      }
    }
    else if (state == OUT) {
      state = IN;
    }
    if (state == IN) {
      putchar(c);
    }
    last = c;
  }
  return 0;
}

That solved it, except now if there is [blank][tab] next to each other, a newline gets printed for both.

Could someone please help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:02:48+00:00Added an answer on May 17, 2026 at 7:02 pm

    Your problem with your original code is that you will output your newline for every whitespace character. You only want to do it when transitioning from word to non-word:

    Change:

    if (c == ' ' || c == '\n' || c == '\t') {
        state = OUT;
        printf("\n");
    }
    

    to:

    if (c == ' ' || c == '\n' || c == '\t') {
        if (state == IN) printf("\n");
        state = OUT;
    }
    

    In fact, what I originally thought I’d suggest would be an enumeration for the states along the lines of:

    enum eState {IN, OUT};
    :
    enum eState state = OUT;
    

    but, for a simple finite state machine with only two states, you can just use an boolean:

    #include <stdio.h>
    
    #define FALSE (1==0)
    #define TRUE  (1==1)
    // Or: enum eBoolean {FALSE = 0, TRUE = 1};
    
    int main (void) {
        int ch;
        int inWord = FALSE;     // Or: enum eBoolean inWord = FALSE;
    
        // Process every character.
        while ((ch = getchar()) != EOF) {
            // Check for whitespace.
            if (ch == ' ' || ch == '\n' || ch == '\t') {
                // Check if transitioning nonwhite to white.
                if (inWord) {
                    printf("\n");
                }
    
                // Mark white no matter what.
                inWord = FALSE;
            } else {
                // Mark non whitespace.
                inWord = TRUE;
            }
    
            // If not whitespace, output character.
            if (inWord) {
                putchar(ch);
            }
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a code that is similar to this: #include <stdio.h> double some_function( double
So here is the original query SELECT SUM(PickQty), SUM(ReqQty), AssignmentID, StopID FROM PickRequest GROUP
Here's a perfect example of the problem: Classifier gem breaks Rails . ** Original
Here is my code, which takes two version identifiers in the form 1, 5,
I am using the Java Native Interface to include some statically compiled code in
So here is my code: <?php $url = http://www.site.com/whcms/includes/api.php; # This is not the
UPDATE: Turns out my code works. Browser was caching previous failed response. Thanks for
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.