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 6805537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:35:56+00:00 2026-05-26T19:35:56+00:00

Possible Duplicate: Consecutive Blank space Removal in C Prompt: A text file contains a

  • 0

Possible Duplicate:
Consecutive Blank space Removal in C

Prompt:
A text file contains a bunch of characters. There are no tab characters within the file. Write a program that replaces two or more consecutive blanks by a single blank. The input from this program should come from a file whose name has been supplied via argv[1]. The output from this program should go to standard output.

Question: How to remove consecutive blank spaces in a text file?

I have started my code and it compiles but then doesn’t do anything. Im not sure where I went wrong with the code. I want the code to basically simulated a FMS which says that if the text read in is equal to a blank space two times in a row then place the text from the file there but I am having trouble getting it to print out correctly.

Input:

Let’s   go  to  the movies.

Output:

Let’s go to the movies.

My written Code:

#include <stdio.h>

int main(int argc, char* argv[]){
    int i;
    char c;
    FILE* fin;

    fin=fopen("textfile38", "r");
    fscanf(fin,"%c", &c);
    while((i=getchar()) !=EOF)
            putchar(c);
    if(i ==' ')
    {
            putchar(i);
    }
    else
    {
            putchar(' ');
    }
    printf("%c \n", c);
    return 0;
}

I get it to return L. Am I not reading in all of the characters? Any help would be appreciated thanks.

UPDATED CODE:

#include <stdio.h>

int main(int argc, char* argv[]){
    char c;
    FILE* fin;

    fin=fopen("textfile38", "r");
    while(fscanf(fin,"%c", &c) !=EOF){
            if(c ==' ')
            {
                    putchar(c);
            }
            else
            {
                    putchar(' ');
            }
            printf("%c", c);
    }
    return 0;
}

Only problem is that the spaces are still there from the input, also its printing vertically not horizontally, and i don’t know why.

  • 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-26T19:35:56+00:00Added an answer on May 26, 2026 at 7:35 pm

    You probably need to keep state as to what you have seen, so something like….

       int seenspace = 0;
       while((i=getchar()) !=EOF) {
       {
           if(i != ' ')
           {
                if (seenspace) 
                   putchar(' ');
                putchar(i);
                seenspace = 0;
           } else {
                if (seenspace)  {
                   putchar(' '); 
                   seenspace = 0; 
                } else {
                   seeenspace = 1;
                }
           }
       }
       if (seenspace) // flush the training blank if any 
          putchar(' '); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Opening url in new tab while i am doing window.open(), my page
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: how to delete a file? My application first reads the fields in
Possible Duplicate: Echo expanded PS1 Is there any way to 'evaluate' PS1 , PS2
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Are there any O(1/n) algorithms? Any algorithm whose complexity decreases with increase
Possible Duplicate: Find out the instance id from within an ec2 machine I am

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.