简体   繁体   中英

Program to find how many words in a text don't contain a specific character

The following program runs without printing anything on the screen, maybe because the loop goes over the null character. I can't understand why and how this happens, and how to fix it.

//program to find how many word in the text doesn't contain p char

#include<stdio.h>
#include<stdbool.h>
#define space ' '

void find_word(char s[]) {
  bool wordfound = false;

  int i, j = 0, word = 0;
  i = 0;

  while (s[i]) { //s[i]!='\0'  does not

    if (s[i] != 'p' && s[i + 1] != space) { //for the first word 
      wordfound = true;
      word++;
    }

    wordfound = false;

    if (s[i] == space && s[i + 1] != space) { //for more than one word in the text 
      for (j = i + 1; s[j] != space; j++)
        if (s[j] != 'p' && s[j + 1] != space)
          wordfound = true;
    }
    if (wordfound) {
      word++;
    }
    wordfound = false;
    i = j;
    i++;
  } //end while loop

  printf("Number of words not contain p character%d\n\n", word);

}

int main(void) {
  char s[] = {"pppp zzzz ppp ssss dfg sfsfdsf"};
  find_word(s);
  return 0;
}

You appear to have your for-loop terminating condition set to be unsatisfiable given your input.

if (s[i] == space && s[i + 1] != space) { //for more than one word in the text 
  for (j = i + 1; s[j] != space; j++)
    if (s[j] != 'p' && s[j + 1] != space)
      wordfound = true;
}

Here you are checking for a leading space in your input string. If you find it you then increment your index checking until you reach another space. What if your string doesn't have a trailing space?

Instead try to have a second condition for null and space to terminate the loop:

if (s[i] == space && s[i + 1] != space) { //for more than one word in the text 
  for (j = i + 1; s[j] != '\0' && [j] != space; j++)
    if (s[j] != 'p' && s[j + 1] != space)
      wordfound = true;
}

And then you set:

    wordfound = false;
    i = j;
    i++;
  } //end while loop

This will keep re-setting your loop, I'm not clear on your reasoning for this but that will run your loop indefinitely.

If you make these edits your code terminates:

#include<stdio.h>
#include<stdbool.h>
#define space ' '

void find_word(char s[]) {
  bool wordfound = false;

  int i, j = 0, word = 0;
  i = 0;

  while (s[i]) { //s[i]!='\0'  does not

    if (s[i] != 'p' && s[i + 1] != space) { //for the first word 
      wordfound = true;
      word++;
    }

    wordfound = false;

    if (s[i] == space && s[i + 1] != space) { //for more than one word in the text 
      for (j = i + 1; s[j] && s[j] != space; j++)
        if (s[j] != 'p' && s[j + 1] != space)
          wordfound = true;
    }
    if (wordfound) {
      word++;
    }
    wordfound = false;
    i++;
  } //end while loop

  printf("Number of words not contain p character%d\n\n", word);

}

int main(void) {
  char s[] = {"pppp zzzz ppp ssss dfg sfsfdsf"};
  find_word(s);
  return 0;
}

Output:

Number of words not contain p character24

There are a few problems with this code, but the main one is that inside the loop you assign j to i which causes the infinite loop as the while(s[i]) condition is never met. Why don't you try to make it simple, like so:

//program to find how many word in the text doesn't contain p char

#include<stdio.h>
#include<stdbool.h>
#define space ' '

void find_word(char s[]) {
  bool is_in = false;
  short words_count = 0, i = 0; 

  while (s[i]) {
      if (s[i] == 'p') { // if this letter is a 'p', mark the word
          is_in = true;
      }

      if (s[i] == space) { // if it's end of word
          if (!is_in) { // check if 'p' is present and increase the count
              words_count++;
          }
          is_in = false;
      }

      i++;
  }

  if (!is_in) { // check if the last word has 'p'
      words_count++;
  }

  printf("no. of words without p is %d\n", words_count);

}

int main(void) {
  char s[] = {"pppp zzzz ppp ssss dfg sfsfdsf"};
  find_word(s);
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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