使用C进行反向字符串比较

时间:2016-04-28 04:12:59

标签: c

我刚刚编译了这个程序,但似乎没有用。

我应该做的是制作一个程序,确定一个单词是一个回文(一个前后相同的单词,例如“racecar”或“eye”)。

它也应该忽略大小写字母(例如Racecar或eYe)。这就是我到目前为止所做的:

int main()

    char word[21],reverse[21];
    printf("Type a word and I will tell you if it is a Palindrome: ");
    fgets(word,21,stdin);
    puts("");
    printf("The word that you typed is: %s \n",word);
    strcpy(reverse,word);
    strrev(reverse);
    if((strcmp(reverse,word))==0)
    printf("This word is a palindrome");
    else
    printf("This word is NOT a palindrome");

    return 0;
}

4 个答案:

答案 0 :(得分:3)

你不需要反转字符串来检查它是否是回文,strrev也不是C标准中的函数。您可以从单词的开头和结尾处进行检查:

char word[21];
printf("Type a word and I will tell you if it is a Palindrome: ");
fscanf(stdin, "%s", word);
puts("");
printf("The word that you typed is: %s \n",word);

size_t len = strlen(word);
int s;
bool palindrome = true;
for (s = 0; s < len / 2; s++) {
    if (tolower(word[s]) != tolower(word[len-1-s])) {
        palindrome = false;
        break;
    }
}
if(palindrome)
    printf("This word is a palindrome");
else
    printf("This word is NOT a palindrome");

这节省了空间和时间。

答案 1 :(得分:2)

fgets()如果遇到一个,则将换行符存储到提供的缓冲区中。键入单词并按Enter键时,“enter”将生成换行符,并将其存储在缓冲区中。当您反转字符串时,换行符将成为该反转的一部分,因此,您将永远不会得到肯定的结果。此外,为了确保比较可以不区分大小写,您可以先将整个单词转换为小写(或大写)。

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

int main()
{
    char word[21] = {0}, reverse[21];

    printf("Type a word and I will tell you if it is a Palindrome: ");
    fgets(word,21,stdin);

    // remove linefeed character if there is one
    size_t len = strlen(word);
    if (len > 0 && word[len - 1] == '\n')
    {
        word[len - 1] = '\0';
        len--;
    }

    // convert string to lowercase
    for (size_t i = 0; i < len; i++)
        word[i] = tolower(word[i]);

    puts("");
    printf("The word that you typed is: %s \n",word);

    strcpy(reverse,word);
    strrev(reverse);

    if((strcmp(reverse,word))==0)
        printf("This word is a palindrome");
    else
        printf("This word is NOT a palindrome");

    return 0;
}

答案 2 :(得分:0)

  • 试试这个简单的代码:
#include <stdio.h>
#include <string.h>

int main()
{
    char word[21] = {0}, reverse[21] = {0};
    int len;
    printf("Type a word and I will tell you if it is a Palindrome: ");
    fgets(word,21,stdin);

     len = strlen(word);

    // convert string to lowercase and remove '\n' feed problem if any
    for (int i = 0; i < len ; i++)
     {
     if(word[i] != '\n')
     word[i] = tolower(word[i]);
     }
     word[i] = '\0';

    puts("");
    printf("The word that you typed is: %s \n",word);

    strcpy(reverse,word);
    strrev(reverse);

    if((strcmp(reverse,word))==0)
        printf("This word is a palindrome");
    else
        printf("This word is NOT a palindrome");

    return 0;

答案 3 :(得分:0)

fgets,也将\ n放在字符串的末尾。 所以,正确的代码将是这样的:

int main()
{

    char word[21],reverse[21];
    printf("Type a word and I will tell you if it is a Palindrome: ");
    fgets(word,21,stdin);    /// Actually it saves: "123\n"
    word[strlen(word)-1] = '\0';
    puts("");
    printf("The word that you typed is: %s \n",word);
    strcpy(reverse,word);
    strrev(reverse);
    if((strcmp(reverse,word))==0)
    printf("This word is a palindrome");
    else
    printf("This word is NOT a palindrome");

    return 0;
}