【问题标题】:Program to find how many words in a text don't contain a specific character查找文本中有多少单词不包含特定字符的程序
【发布时间】:2018-09-01 14:24:19
【问题描述】:

以下程序运行时没有在屏幕上打印任何内容,可能是因为循环越过了空字符。我不明白为什么会发生这种情况,如何发生,以及如何解决。

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

【问题讨论】:

  • 为了便于阅读和理解:1)请遵循公理:*每行只有一个语句,并且(最多)每个语句一个变量声明。 2)单独的代码块:forifelsewhiledo...whileswitchcasedefault通过一个空行。 3) 用 2 或 3 个空行分隔函数(保持一致) 4) 请一致地缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '}' 之前取消缩进。建议每个缩进级别为 4 个空格。
  • 你可能想查看头文件:ctypes.h,尤其是isspace()ispunc()
  • 关于:printf("Number of words not contain p character%d",word); 这会将数据留在stdout 缓冲区中,直到程序退出。要将其立即显示在终端上,请以 '\n' (新行)结束格式字符串,因为这会将 stdout 缓冲区刷新到终端
  • s[i + 1]空字符 时,if (s[i] == space &amp;&amp; s[i + 1] != space) { for (j = i + 1; s[j] != space; j++) if (s[j] != 'p' &amp;&amp; s[j + 1] != space) 会出现问题。内部循环尝试访问外部字符串。这个内部的j 循环似乎在其他情况下也会继续运行。
  • Jassim Alsokni,“单词”检测很弱。考虑"no_spaces"" leading_spaces""trailing_spaces "" """。让你的字数在这种情况下起作用,然后修改代码以查找'p'Divide and Conquer

标签: c string search


【解决方案1】:

这段代码有一些问题,但主要问题是在循环内你将 j 分配给 i 导致无限循环作为 while(s[i]) 条件永远不会满足。你为什么不试着让它变得简单,像这样:

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

【讨论】:

    【解决方案2】:

    根据您的输入,您似乎将for-loop 终止条件设置为无法满足。

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

    然后你设置:

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

    这将不断重新设置您的循环,我不清楚您对此的推理,但这会无限期地运行您的循环。

    如果您进行这些编辑,您的代码将终止:

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

    输出:

    Number of words not contain p character24
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多