连续的空格去除用C [英] Consecutive Blank space Removal in C

查看:97
本文介绍了连续的空格去除用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个文本文件包含一串字符。有文件中没有制表符。编写一个程序,用一个空格替换两个或多个连续的空格。该程序的输入应该来自他的名字已经通过的argv [1]提供的文件。该程序的输出应该去到标准输出。

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.

输入:

Let’s   go  to  the movies.

输出:

Let’s go to the movies.

这是我迄今为止:

#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.

推荐答案

这可能会帮助你进步:

看来你读之前 while循环只有一个字符的。你可能想读 while循环中连续字符并停止循环到达文件结尾时。

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?

这篇关于连续的空格去除用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆