小编Yuv*_*val的帖子

strcmp仅返回0(palindrom algo bug)

嗨,我在课堂上学到了string.h库,特别是strcmp函数,它比较了字符串.如果第一个字符串首先出现在字典中,它将返回一个大于0的数字,如果第二个字符串大于第一个字符串将返回小于0的数字,如果它们是等于它应该返回0. ive像这样使用它:

strcmp(strArr , strrev(strArr));
Run Code Online (Sandbox Code Playgroud)

随意教育我.

代码 :

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

#define MAX_LENGTH 100
#define PALINDROM_TRUE 0//because in strcmp if both strings compared are equile the strcmp will return 0.
int main(void)
{
    char strArr[MAX_LENGTH];
    printf("Enter string (max length 100 chars): ");
    fgets(strArr , MAX_LENGTH , stdin);
    int pali = strcmp(strArr , strrev(strArr));
    if(pali == PALINDROM_TRUE)
    {
        printf("Palindrom\n");
    }
    else
    {
        printf("Not Palindrom\n");
    }
    system("PAUSE");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我进入即下面的代码"ABC"它输出到屏幕上回文它应该打印不回文,它永远不会打印不回文

c strcmp string.h

0
推荐指数
1
解决办法
243
查看次数

标签 统计

c ×1

strcmp ×1

string.h ×1