简体   繁体   English

在C中检查空白程序无法正常工作

[英]Checking for whitespace program in C not working properly

I am trying to check for sequential whitespace in a file full of characters. 我正在尝试在充满字符的文件中检查顺序空格。 I want my program to ignore more than 1 whitespace after a sequence of characters. 我希望我的程序在一个字符序列后忽略多个空格。 Also, a tab will be replaced for a whitespace. 另外,制表符将替换为空格。 I am opening a file and reading it, so don't worry about that part of the code, since it works. 我正在打开文件并阅读它,所以不要担心代码的那部分,因为它可以工作。 My code: 我的代码:

char ch;
char sentenceArray[1000];
int charCount = 0;

    while (1) {
        ch = getc(file);

        //If is some sort of space, check it
        if(ch == ' '){
            if(sentenceArray[charCount-1] != ' '){
                sentenceArray[charCount] = ' ';
            }
        }else if(ch == '\t'){
            if(sentenceArray[charCount-1] != ' '){
                sentenceArray[charCount] = ' ';
            }
        }else{
            printf("Not space");
            sentenceArray[charCount] = ch;
        }
        charCount++;
    }

void print()
{
    int i;
    for(i = 0; i<= charCount; i++){
        printf("%c", sentenceArray[i]);
    }
}

The only relevant line in main is: main中唯一相关的行是:

print();

If I feed it a file: 如果我给它提供文件:

myprog < file1

The contents of my file look like: 我文件的内容如下:

Uno Dos Tres Cuatro a

Where the spaces are 1 in between Uno and Dos, 2 in between Dos and Tres, 3 in between Tres and Cuatro, and a tab in between Cuatro and a. 其中在Uno和Dos之间的空格是1,在Dos和Tres之间的空格是2,在Tres和Cuatro之间的空格是3,在Cuatro和a之间的空格是。

This is the output (I print the array): 这是输出(我打印数组):

Uno Dos Tres Cuatro a

As you can see, my program successfully eliminates just 2 continuos spaces... if they are more, it just keeps on deleting two but if they are more, say 10, it only takes 2 out and then it prints 8 spaces. 如您所见,我的程序成功地消除了2个连续空格...如果更多,它将继续删除两个,但如果更多,例如10,则只删除2个空格,然后打印8个空格。

Do you know why this is happening? 你知道为什么会这样吗? What are the flaws in my code? 我的代码有哪些缺陷?

Thanks! 谢谢!

You are incrementing charCount every time you get a new character. 每次获得一个新字符时,您都在递增charCount You should only be updating charCount when adding a new char to your output. 仅应在将新的char添加到输出中时更新charCount

Otherwise you are going to be comparing to an unknown (or whatever sentenceArray is initialized to) value after the second space is encountered which will cause the check if(sentenceArray[charCount-1] != ' ') to result in true and add another space. 否则,在遇到第二个空格后,您将与一个未知值(或初始化为任何sentenceArray if(sentenceArray[charCount-1] != ' ') )进行比较,这将导致检查if(sentenceArray[charCount-1] != ' ') true并添加另一个空间。

  //If is some sort of space, check it
    if ((ch == ' ') || (ch == '\t')){
        if((charCount == 0) || (sentenceArray[charCount-1] != ' '))
        {
            sentenceArray[charCount] = ' ';
            charCount++; // <-- added this here
        }
    }else{
        printf("Not space");
        sentenceArray[charCount] = ch;
        charCount++; // <-- added this here
    }
    // charCount++; <-- remove this

On a side note, you may want to look at using isspace() 附带一提,您可能想看看使用isspace()

Code needs to keep track if the previous char was a white-space. 如果前一个char为空格,则代码需要保持跟踪。

// char ch;
int ch;
char sentenceArray[1000];
int charCount = 0;
int previous_space = 0;

while ((ch = getc(file)) != EOF && charCount < 1000) {

    if (isspace(ch)) {
      if (!previous_space) {
        sentenceArray[charCount++] = ' ';
        previous_space = 1;
        }
      }
    else {
      sentenceArray[charCount++] = ch;
      previous_space = 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM