Tag: strcmp

如何检查字符串是否在文件中

我是C的新手,我正在试图弄清楚如何在一个包含多个数字和单词的文件中搜索特定字符串及其后面的数字。 我正在使用的输入文件如下所示: TREES 2 BENCHES 5 ROCKS 10 PLANTS 8 我知道如何使函数读取字符串,我知道如何比较两个字符串但我不知道如何将它们放在一起或如何设置数组以读取整个文件。 我一直在使用strcmp ,但我只是不知道如何初始化我想要寻找的这个词。

比较C – strcmp中的字符串

我在比较C中的字符串时遇到了麻烦(我很擅长)。 我在此服务器应用程序上有套接字等待接受来自客户端的数据。 在我程序的这个特定部分,我希望能够根据从客户端收到的数据执行MySQL查询。 我希望能够知道接收到的数据何时具有“newuser”的值以启动简单的注册过程。 Strcmp返回一个正值1,我相信我应该得到一个0,因为值应该相等。 源代码: //setup socket //loop and select structure to handle multiple connections if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) { // got error or connection closed by client if (nbytes == 0) { // connection closed printf("selectserver: socket %d hung up\n", i); } else { perror("recv"); } close(i); […]

将字符串数组传递给函数分段错误

所以,我在下面编写了这个代码,它应该将一个字符串数组传递给一个函数,然后将该数组按字母顺序排序。 我知道我做的方式可能并不漂亮,但它适用于学校,我需要将它传递给函数并使用strcmp 。 我遇到了一些问题,但我设法将所有编译错误排序。 但是,现在,当我尝试运行程序时,我得到错误segmentation fault(core dumped) 。 有人能引导我到我犯错误的地方吗? #include #include #include #include void sort(char *str[]); int main() { char *states[11] = {“Florida”, “Oregon”, “California”, “Georgia”}; sort(states); return 0; } void sort(char *str[]) { int x, y; char alpha[11] = {0}; for(x = 1; x < 4; x++){ for(y = 1; y 0){ strcpy(alpha, str[y […]

为什么我的strcmp()失败了?

我是一个C新手和学习字符串标记。 我试图通过以下方式比较两个字符串。 但我正在做的字符串比较是失败的。 你能告诉我这里缺少什么吗? 我找不到另一个类似的问题,可能是由于我对C的经验不足。如果存在,你可以请我转发它吗? char* input = “comparer here”; char* args[5]; int counter = 0; char *tok = strtok(input, ” “); while (tok != NULL) { args[counter] = tok; counter ++; if (counter == 5) break; tok = strtok(NULL, ” “); } char* comp_str = “comparer”; if (strcmp(args[0], comp_str) == 1) { // do […]

用于qsort函数指针的Typecast

#include #include #include #include static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are “pointers to pointers to char”, but strcmp(3) arguments are “pointers to char”, hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const *) p2); } int […]

比较两个字符串,strcmp的问题

我正在尝试检查从stdin读取的行是否以“login:”开头,但strcmp似乎不起作用。 char s1[20], s2[20]; fgets(s1, 20, stdin); strncpy(s2,s1,6); strcmp(s2, “login:”); if( strcmp(s2, “login:”) == 0) printf(“s2 = \”login:\”\n”); else printf(“s2 != \”login:\”\n”); 我不关心“login:”之后会发生什么,我只是想确定命令是如何给出的。 我究竟做错了什么?

strcmpfunction无法正常工作

我在结构books数组上有一个deletefunction 。 我正在传递一系列记录, author of book name of book和size of the list 。 现在这里给出了list[0].author和list[5].author和author all等于“Dan Brown”(同一个字符串) void delete(struct books *list,char author[],char name[],int n) { int i,a; a=strcmp(list[0].author,list[5].author); printf(“%d\n”,a); // prints 0 a=strcmp(list[0].author,author); printf(“%d\n”,a); // prints other than 0 } 为什么会这样? 这有什么不对?

C中的strcasecmp返回156而不是0,任何想法为什么?

我有以下代码: printf(“num: %d\n”, strcasecmp(buf, “h\n”)); 当我尝试插入不同的字母时,我得到以下结果: a: -7 g: -1 i: 1 j: 2 h: 156 H: 156 当buf等于H或h时, strcasecmp不应该返回0吗? 任何想法为什么它返回156? 我需要弄清楚如何检查用户是否键入H或h 。 谢谢! 编辑:我正在通过以下方式读取buf: read(0, buf, MAXBUFLEN);

strcmpi重命名为_strcmpi?

在MSVC ++中,有一个函数strcmpi用于不区分大小写的C字符串比较。 当你尝试使用它时,它会, 从Visual C ++ 2005开始,不推荐使用此POSIX函数。请改用ISO C ++符合_stricmp。 我没有看到的是为什么ISO不希望MSVC ++使用strcmpi,为什么_stricmp是首选方式,为什么他们懒得重命名函数,以及函数如何以符合下划线的ISO开头。 我知道所有这些都必须有一个原因,我怀疑它是因为strcmpi是非标准的,也许ISO希望非标准扩展以_underscore开头?

c – strcmp对于相等的字符串不返回0

所以我试图广泛地搜索这个解决方案,但只能找到其中一个字符串中缺少新行或空字节的post。 我很确定这不是这种情况。 我使用以下函数将一个单词与一个文件进行比较,该文件包含每行一个单词的单词列表(函数中的字典)。 这是代码: int isWord(char * word,char * dictionary){ FILE *fp; fp = fopen(dictionary,”r”); if(fp == NULL){ printf(“error: dictionary cannot be opened\n”); return 0; } if(strlen(word)>17){ printf(“error: word cannot be >16 characters\n”); return 0; } char longWord[18]; strcpy(longWord,word); strcat(longWord,”\n”); char readValue[50] = “a\n”; while (fgets(readValue,50,fp) != NULL && strcmp(readValue,longWord) != 0){ printf(“r:%sw:%s%d\n”,readValue,longWord,strcmp(longWord,readValue));//this line is […]