简体   繁体   English

strchr在C中不起作用

[英]strchr not working in C

So right now I'm trying to code a program in C that takes a string and checks for proper punctuation (eg ends with '.', '?', or '!'). 因此,现在我正在尝试使用C编写一个程序,该程序采用字符串并检查正确的标点符号(例如,以“。”,“?”或“!”结尾)。 I am trying to use the strchr function to test and see if the last character in the string is one of the punctuation marks using a if loop within a for loop. 我正在尝试使用strchr函数进行测试,并使用for循环中的if循环查看字符串中的最后一个字符是否为标点符号之一。 However when I run the program it seems to skip the if loop all together. 但是,当我运行程序时,似乎一起跳过了if循环。

Here is the program: 这是程序:

#include<stdio.h>
#include<string.h>

int main(void)
{
 char string[1000];
 int i,length,a,p,q,e;

 printf("Please enter a sentence with a valid punctuation.\n\n");


 for(i=0;i<1000;i++)
 {
  fgets(string,2,stdin);
  p=strchr(string,'.');
  q=strchr(string,'?');
  e=strchr(string,'!');
  if(string[sizeof(string)-1]=='.'||'?'||'!')
  {
   printf("\nYay a sentence!");
   break;
  }
  else if((p && q && e)==NULL)
  { 
   printf("You didn't provide any punctuation. Goodbye.");
   exit(a);
  }
 }
 printf("You entered the sentence:\n %s",string);

 return 0;
}

I have tried it so many different ways such as trying strstr instead or even storing it a different way through gets (which I quickly learned through gcc and some research is not the way to go.) 我已经尝试了很多不同的方法,例如尝试使用strstr或什至通过gets来存储它(我通过gcc很快就学到了,有些研究并不是路要走。)

I am just completely lost on why this isn't working when I enter a sentence without punctuation. 当我输入不带标点的句子时,我完全迷失了为什么它不起作用。

Sorry if this is really simple, I am fairly new to this. 抱歉,如果这真的很简单,我对此并不陌生。

Thanks in advance. 提前致谢。

You misunderstood the return value of strchr : it does not return an index of the character; 你误解的返回值strchr :它返回字符的索引; instead, it returns a pointer to the character that you search. 而是返回指向您搜索的字符的指针

char *p=strchr(string, '.');
char *q=strchr(string, '?');
char *e=strchr(string, '!');

In addition, sizeof does not return the actual length of the string; 此外, sizeof 返回字符串的实际长度; it returns 1000, which is the size of the string array. 它返回1000,这是string数组的大小。 You need to use strlen instead. 您需要改用strlen

Finally, string[strlen(string)-1]=='.'||'?'||'!' 最后, string[strlen(string)-1]=='.'||'?'||'!' does not compare the last character to one of three characters. 不会将最后一个字符与三个字符之一进行比较。 It always returns 1 , because character codes of ? 它始终返回1 ,因为字符代码为? and ! ! are not zero, and so the logical OR || 不为零,因此逻辑OR || operator treats them as true value. 运算符将其视为true值。

Same goes for (p && q && e)==NULL) condition: it does not check that all three values are NULL ; (p && q && e)==NULL)条件也是如此:它不检查所有三个值是否均为NULL one of them being NULL would be sufficient to produce an equality, but it's not what you want. 其中之一为NULL就足以产生相等,但这不是您想要的。

Here is how you fix this: 解决方法如下:

char last = string[strlen(string)-1];
// Skip '\n's at the end
while (last != 0 && (string[last] == '\n' || string[last] == '\r')) {
    last--;
}
if (last == '.' || last == '?' || last == '!') {
    ...
}
// Using implicit comparison to NULL is idiomatic in C
if (!p && !q && !e) {
    ...
}
  • strchr returns pointer. strchr返回指针。
  • expressions like hoge==a||b||c are strange. hoge==a||b||c这样的表达式很奇怪。 You have to write them separately. 您必须分别编写它们。

fixed code: 固定代码:

#include<stdio.h>
#include<string.h>

int main(void)
{
 char string[1000];
 int i,length,a;
 char *p,*q,*e;

 printf("Please enter a sentence with a valid punctuation.\n\n");


 for(i=0;i<1000;i++)
 {
  fgets(string,2,stdin);
  p=strchr(string,'.');
  q=strchr(string,'?');
  e=strchr(string,'!');
  if(string[sizeof(string)-1]=='.'||string[sizeof(string)-1]=='?'||string[sizeof(string)-1]=='!')
  {
   printf("\nYay a sentence!");
   break;
  }
  else if(p==NULL&&q==NULL&&e==NULL)
  { 
   printf("You didn't provide any punctuation. Goodbye.");
   exit(a);
  }
 }
 printf("You entered the sentence:\n %s",string);

 return 0;
}

Note: 注意:

  • string[sizeof(string)-1] will be indeterminate because it means string[999] and it will never be written because you tell fgets that string has only 2 elements. string[sizeof(string)-1]是不确定的,因为它表示string[999] ,并且永远不会被写入,因为您告诉fgets string只有2个元素。
  • a used as the argument of exit is indeterminate. a用作参数exit是不确定的。 Please initialize it. 请初始化它。
  • length is unused. length未使用。

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

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