This question already has answers here:
Check if a string is palindrome in C
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我已经搜索并找到了解决方案,但我无法弄清楚为什么我的代码仍然无法正常工作,如果我正确理解我的check2字符串没有'\ 0'的原因。
我正在使用Visual Studio
我知道有一种更好的写作方式,比这短,但只是尝试练习一些东西,然后尝试按需而努力。
我是C的新手,所以不要判断我:P
这是我的代码:

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

/*
A palindrome is a string that is same in both forward and backward reading.
Example:
   "madam"
   "racecar"
   "a man a plan a canal panama"
   "radar"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
        OR
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.

*/


int main(){
    char string [100];
    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    char check1[100], check2[100];
    int i, j = 0;

    for (i = 0; string[i] != '\0'; i++, j++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[i] = string[i];
        }
    }

    check1[i] = '\0';

    for (i = strlen(string); i < 0; i--)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check2[i] = string[i];
        }
    }

    i = strlen(string);
    check2[i] = '\0';

    for (i = 0; check1[i] != '\0'; i++)
    {
        if ((check1[i] >= 65) && (check1[i] <= 90))
            check1[i] = check1[i] + 32;
    }

    for (i = 0; check2[i] != '\0'; i++)
    {
        if ((check2[i] >= 65) && (check2[i] <= 90))
            check2[i] = check2[i] + 32;
    }

    printf("%s\n", check1);
    printf("%s\n", check2);

    isPalindrome = strcmp(check1, check2);

    // at the end you need to test
    if (isPalindrome){
        printf("Yes, it is Palindrome!\n");
    }
    else{
        printf("No, not a Palindrome\n");
    }


    return 0;
}


这是我的输出:

Enter a string: MadaM
madam
╠╠╠╠╠
Yes, it is Palindrome!
Press any key to continue . . .


好的,现在我的代码如下所示:

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

/*
A palindrome is a string that is same in both forward and backward reading.
Example:
   "madam"
   "racecar"
   "a man a plan a canal panama"
   "radar"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
        OR
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.

*/


int main(){
    char string [100];
    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    char check1[100], check2[100];
    int i, j, dess = 0, len;

    for (i = 0; string[i] != '\0'; i++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[dess++] = string[i];
        }
    }

    check1[dess] = '\0';



    /*
    len = strlen(string);


    for (j = 0, i = 0; j < len; j++)
    {
        if ((string[j] >= 'A' && string[j] <= 'Z') || (string[j] >= 'a' && string[j] <= 'z'))
        {
            check2[i++] = string[len - j];
        }
    }
    */

    for (dess = 0, i = strlen(string) - 1; i >= 0; i--)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check2[dess++] = string[i];
        }
    }

    check2[dess] = '\0';

    for (i = 0; check1[i] != '\0'; i++)
    {
        if ((check1[i] >= 65) && (check1[i] <= 90))
            check1[i] = check1[i] + 32;
    }

    for (i = 0; check2[i] != '\0'; i++)
    {
        if ((check2[i] >= 65) && (check2[i] <= 90))
            check2[i] = check2[i] + 32;
    }

    printf("%s\n", check1);
    printf("%s\n", check2);
    for (i = 0; check1[i] != '\0'; i++)
    {
        printf("%c\n", check1[i]);
    }
    for (i = 0; check2[i] != '\0'; i++)
    {
        printf("%c\n", check2[i]);
    }

    isPalindrome = strcmp(check1, check2);

    // at the end you need to test
    if (isPalindrome){
        printf("Yes, it is Palindrome!\n");
    }
    else{
        printf("No, not a Palindrome\n");
    }


    return 0;
}


但我的输出是:

Enter a string: MadaM
madam
madam
m
a
d
a
m
m
a
d
a
m
No, not a Palindrome
Press any key to continue . . .


为什么说它不是回文?

最佳答案

这个循环根本没有执行:

for (i = strlen(string); i < 0; i--)
{
    if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
    {
        check2[i] = string[i];
    }
}


因为i < 0条件是“继续”条件,而不是“停止”条件。此外,i从外部字符串数据开始。

因此check2从未初始化,这解释了垃圾情况。

固定循环为:

for (i = strlen(string) - 1; i >= 0; i--)


这些反向循环结构容易出错。我建议使用一个正向循环并在循环中计算反向索引,更清楚一点,像这样:

int len = strlen(string);
for (j = 0; j < len; j++)
{
    i = len-j-1;   // starts at the last char, ends at the first char


在这种情况下,不需要反向循环,因为正向循环可以实现完全相同的结果(此处没有break或其他索引)

关于c - 在C中打印时得到[,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51907646/

10-10 22:55