Consecutive Blank space Removal in C [closed]

2019-03-07 18:09发布

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.

Input:

Let’s   go  to  the movies.

Output:

Let’s go to the movies.

This is what i have so far:

#include <stdio.h>

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

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

It doesn't give me an output I'm not sure what I did wrong if anyone could help me NOT just give me the answer that would be great thank you.

标签: c whitespace
1条回答
一夜七次
2楼-- · 2019-03-07 18:24

This might help you to progress:
It seems you read only one character before the while loop. You might want to read characters continuously inside the while loop and stop looping when end of file is reached.

Second hint:
You leave the file open. What should you do before exiting your program?

查看更多
登录 后发表回答