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

  • SEARCH
  • Home
  • 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 1093127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:47:04+00:00 2026-05-16T23:47:04+00:00

Problem: I need to be able identify when two whitespaces occur consecutively. I have

  • 0

Problem: I need to be able identify when two whitespaces occur consecutively.

I have read the following questions:

how to read a string from a \n delimited file

how to read scanf with spaces

And I am aware of scanf problems: http://c-faq.com/stdio/scanfprobs.html

Input will be in the following format:

1 5 3 2  4 6 2  1 9  0

Two white spaces indicates that the next set of data needs to be handle and compared to itself. The length of the line is unknown and the number or integers in each group is unknown. Two whitespaces is the most that will separate the next data set.

While I can use fgets and various built in functions to solve this problem, I am at the point where solving the problem with scanf at this point will likely be easier. However, if that’s not the case, using fgets, strtok and atoi will do most of the job but I still need to identify two whitespaces in a row.

The below will take integers until a non-integer is inputed.

while ( scanf ( "%d", &x ) == 1 )

What I need it do is read whitespaces as well and if there is two consecutive whitespaces I’ll the program to do something different with the next set of data.

And once I do get a white space I don’t know how to say:

if ((input == "whitespace") && (previousInput == "whitespace"))
  ya da ya da
else (input == "whitespace")
  ya da ya da
else 
  ya da ya da

I appreciate your time and thank you for your help.

Lesson learned:
While a solution for scanf is posted below by Jonathan Leffler, the solution was a bit more straightforward with getc (by way of requiring less intimate knowledge of the inner scanf, regular expressions and char). In retrospect better knowledge of regular expressions, scanf and char would of made the problem easier and of course knowing what functions are available and which one would have been the best one to use from the start.

  • 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-16T23:47:05+00:00Added an answer on May 16, 2026 at 11:47 pm

    getc and ungetc are your friends

    #include <stdio.h>
    
    int main(void) {
      int ch, spaces, x;
      while (1) {
        spaces = 0;
        while (((ch = getc(stdin)) != EOF) && (ch == ' ')) spaces++;
        if (ch == EOF) break;
        ungetc(ch, stdin);
        if (scanf("%d", &x) != 1) break;
        printf("%d was preceded by %d spaces\n", x, spaces);
      }
      return 0;
    }
    

    Demo at http://ideone.com/xipm1

    Edit Rahhhhhhhhh … I uploaded that as C++. Here’s the exact same thing, but now C99 strict( http://ideone.com/mGeVk )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I need to write/read objects from a file.This because I need to write/read
I have problem i need to convert from my Array structure to std::vector<int> ...
I have the following problem: I need to use XSLFO to generate a 2-column
I have the next problem: I need to process only 1 request at a
I have an entity that is made up of properties from two different tables
I'm trying to implement a simple TPH example from http://msdn.microsoft.com/en-us/library/dd793152.aspx . I have two
Goal: Gain datatype date with year and month Problem: Need to help to convert
The problem: I need to ensure that a particular directory hierarchy exists in the
My problem : I need to create draggable widgets (here it's a jslider for
To solve some problem I need to compute a variant of the pascal's triangle

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.