J Tarantino
J Tarantino

Reputation: 75

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:

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.

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.

Do you know why this is happening? What are the flaws in my code?

Thanks!

Upvotes: 1

Views: 93

Answers (2)

bentank
bentank

Reputation: 616

You are incrementing charCount every time you get a new character. You should only be updating charCount when adding a new char to your output.

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.

  //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()

Upvotes: 2

chux
chux

Reputation: 153338

Code needs to keep track if the previous char was a white-space.

// 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;
    }
}

Upvotes: 0

Related Questions