尝试创建一个程序来检查回文和 semordinlap

Trying to create a program to check palindromes and semordinlap

我正在尝试创建一个程序来检查给定的字符串是回文还是 emordinlap(或反向对),但是当 运行 我的程序输出一个字符串是回文但不是反向对(例如 racecar 应该都是)。

我已经在这个计算屏幕上调试和 starring 了 3 个小时,但我不知道发生了什么。我的第一直觉是它来自 checkPali 函数,所以我将它分配给指针,不幸的是,同样的问题。

我的第二个猜测是到处都放 printf 语句(我清理了它们),也没有运气。

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

static void *helpPage(void)
{
    puts("usage: epi -h -t -s [string] [file]\n");
    puts("-h       Print this help and exit\n");
    puts("-t       Run the test strings.\n");
    puts("-s       Input a string to check if it is either a palindrome or");
    puts("         a emordinlap, followed by the path to a file.");;
}

static char *reverse(char *str)
{
    // reverse a given string

    char tmp, *src, *dst;
    size_t len;

    if (str != NULL)
    {
        len = strlen(str);
        if (len > 1)
        {
            src = str;
            dst = src + len - 1;

            while (src < dst)
            {
                tmp = *src;
                *src++ = *dst;
                *dst-- = tmp;
            }
        }
    }
    return str;
}

static char *strip(char *s)
{
    // strip a string of a new line

    return strtok(s, "\n");
}

static bool checkEpi(char *reversed, char *filePath)
{
    // check if the word matches in a given file
    // or if the word is an emordnilap

    FILE *wordList;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;

    wordList = fopen(filePath, "r");
    if (wordList == NULL)
    {
        perror("Failed to open file: "); // file probably doesn't exit
    }
    while ((read = getline(&line, &len, wordList)) != -1) // read the file line by line
    {
        if (strip(line) == reversed)
        {
            return true; // return true if the word matches
        }
    }
    fclose(wordList);
}

static bool checkPali(char *origin, char *reversed)
{
    // check if a given word is a palindrome or not

    if (*origin == *reversed)
        return true;
}

static void checkAll(char *origin, char* reverse, char *filePath)
{
    // basically a main function to check if it's a palindrome or a emordnilap

    bool paliRes = checkPali(origin, reverse);
    bool epiRes = checkEpi(reverse, filePath);
    if (paliRes == true)
    {
        printf("\n%s is a palindrome, it is the same forward and backwards\n", origin);
    }
    else
    {
        printf("\n%s is not a palindrome, it is not the same forward and backwards\n", origin);
    }

    if (epiRes == true)
    {
        printf("Reverse of %s is a emordinlap, it spells a word backwards.\n\n", origin);
    }
    else
    {
        printf("Reverse of %s is not a emordinlap, it does not spell a word backwards\n\n", origin);
    }
}

int main(int argc, char *argv[])
{
    if (argv[1] == NULL)
    {
        puts("\nYou failed to pass a valid flag...\n");
        helpPage();
        return 1;
    }
    else
    {

        char *testStrings[] = {"a", "ab", "abc", "another", "cbc", "|0|", "palindrome"};
        int i;
        char s[10000];
        char *defaultWordList = "/usr/share/dict/american-english";
        size_t optInt;

        for (optInt = 1; optInt < argc && argv[optInt][0] == '-'; optInt++)
        {
            switch(argv[optInt][1])
            {
                case 't':
                {
                    for (i = 0; i < sizeof(testStrings) / sizeof(testStrings[0]); i++)
                    {
                        strcpy(s, testStrings[i]);
                        char *origin = testStrings[i];
                        char *revStr = reverse(s);
                        checkAll(origin, revStr, defaultWordList);
                    }
                    return 0;
                }

                case 's':
                {
                    if (argv[2] == NULL)
                    {
                        puts("\nYou must provide a string to test.\n");
                        helpPage();
                        return 1;
                    }
                    else if (argv[3] == NULL)
                    {
                        puts("\nYou must pass a valid file path to use as a wordlist.\n");
                        helpPage();
                        return 1;
                    }
                    else
                    {
                        //strcpy(s, argv[2]);
                        char *origin = argv[2];
                        char *revStr = reverse(argv[2]);
                        checkAll(origin, revStr, argv[3]);
                        return 0;
                    }
                }

                case 'h': helpPage(); return 0;
            }
        }
        return 0;
    }
}

我的陈述没有正确比较,我做错了什么?

在 C 中使用 == 无法有意义地比较字符串。

if (*origin == *reversed)

这只是比较两个参数中每一个的第一个字符。请尝试 strcmp

static bool checkPali(char *origin, char *reversed) 
{
    /* match if result of strcmp is 0 */
    return strcmp(origin, reversed) == 0;  
}

您需要为

进行类似的更改
if (strip(line) == reversed)