C可以假定我要存储哪个数组中的字符吗?

C可以假定我要存储哪个数组中的字符吗?,c,arrays,pointers,c-strings,palindrome,C,Arrays,Pointers,C Strings,Palindrome,我正在编写代码来检查数组是否为回文: 编写一个程序读取消息,然后检查它是否是回文 (信息中的字母从左到右与从右到左相同): 输入一条消息:他像魔鬼一样生活,是吗? 回文的 输入消息:女士,我是亚当。 不是回文 当我进入时,他像魔鬼一样生活,嗯?, 它给我的是输出而不是回文, 但是真正的输出应该是回文 下面的代码是我迄今为止尝试过的代码 #include <stdio.h> #include <ctype.h> #define MAX_LEN 100 int main

我正在编写代码来检查数组是否为回文:

编写一个程序读取消息,然后检查它是否是回文
(信息中的字母从左到右与从右到左相同):

输入一条消息:他像魔鬼一样生活,是吗?
回文的

输入消息:女士,我是亚当。
不是回文

当我进入
时,他像魔鬼一样生活,嗯?

它给我的是输出
而不是回文

但是真正的输出应该是回文

下面的代码是我迄今为止尝试过的代码

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

#define MAX_LEN 100

int main(void) {

    char message[MAX_LEN];
    char c, *p = message, *q;

    printf("Enter a message: ");

    while ((c = toupper(getchar())) != '\n' & p < message + MAX_LEN) {
        if (isalpha(c))
            *p++ = c;
    }
    p--;

    for (q = message; q < p; q++, p--) {
        if (*p != *q) {
            printf("Not a palindrome\n");
            return 0;
        }
    }
    printf("Palindrome\n");
    return 0;
}
#包括
#包括
#定义最大长度100
内部主(空){
字符消息[MAX_LEN];
字符c,*p=message,*q;
printf(“输入消息:”);
while((c=toupper(getchar())!='\n'&p
在检查回文之前,必须删除空格和标点符号。例如,如果使用
civic?
,则它不是回文,因为
。另一方面,如果使用
civ ic
,则由于空白,它不是回文。有

  • 将所有字母转换为大写或小写
  • 删除空白
  • 删除标点符号
  • 是否检查回文
  • 您可以使用
    #include

  • 首先,您必须使用
    scanf()
    ,它接受带空格的字符串
  • 然后您必须将该字符串转换为大写或小写,因为
    a!=A
    。我们知道
    civic
    是回文,但
    civic
    不是回文('civic!=civic),因为大写字母具有不同的ASCII值,小写字母具有不同的ASCII值

    • (a-z)-:97-122
    • (A-Z)-:65-90
  • 就我而言,我已将小写字母转换为大写字母

    while(strlen(word) >= i)
        {
            if(word[i] >= 97 && word[i] <= 122) 
            {
                word[i] = word[i] - 32; 
            }
    
            i++;
        }
    
  • 最后一件事是我们必须反转字符串,并且需要检查反转的字符串是否等于原始字符串。如果这是真的,我们的字符串是回文的。如果为false,则字符串不是回文 第二次测试输出-:

    Enter a string = He lived as a devil, eh?
    After converting it to uppercase = HE LIVED AS A DEVIL, EH?
    After removing spaces = HELIVEDASADEVILEH
    HELIVEDASADEVILEH is a palindroeme
    
    Enter a string = Madam I am Adam.
    After converting it to uppercase = MADAM I AM ADAM.
    After removing spaces = MADAMIAMADAM
    MADAMIAMADAM is not a palindrome
    

    首先,您应该声明变量
    c
    的类型为
    int
    。用户可以中断输入过程,在这种情况下,函数
    getchar
    返回整数值
    EOF
    ,您应该检查是否发生这种情况

    char *p = message, *q;
    int c;
    
    while语句的条件中存在错误

    while ((c = toupper(getchar())) != '\n' & p < message + MAX_LEN) {
    
    调用
    toupper
    isalpha
    的参数应转换为unsigned char类型。否则,通常在不强制转换的情况下,这样的调用可以调用未定义的行为

    最好不要从输入的字符串中排除数字。因此,最好至少调用函数
    isalnum
    ,而不是函数
    isalpha

    在这种情况下,用户可以输入一个空字符串,即指针的减量

    p--;
    
    也可以调用未定义的行为

    当一个程序只有一个退出点时,效果更好

    程序可以按以下方式运行

    #include <stdio.h>
    #include <ctype.h> 
    
    #define MAX_LEN 100
    
    int main(void) 
    {
        char message[MAX_LEN];
    
        printf( "Enter a message: " );
    
        char *p = message;
    
        for ( int c; p < message + MAX_LEN &&  ( c = getchar() ) != EOF && c != '\n';   )
        {
            if( isalnum( ( unsigned char )c ) )
            {
                *p++ = toupper( ( unsigned char )c );
            }           
        }
    
        int palindrome = 1;
    
        if ( p != message )
        {
            for ( char *q = message; palindrome && q < --p; ++q )
            {
                palindrome = *q == *p;
            }           
        }
    
        printf( "The entered message is %spalindrome\n", 
                palindrome ? "" : "not " );
    
        return 0;
    }
    
    或者

    Enter a message: Madam, I am Adam
    The entered message is not palindrome
    
    请注意,不要使用包含大量函数调用的循环
    getchar
    ,您只能使用一个函数调用
    fgets

    fgets( message, sizeof( message ), stdin );
    


    您正在通过
    p
    消息
    写信。编写到<代码> *P<代码>等同于写入<代码>消息[i] <代码>,其中<代码> i>代码>介于0和 Max(1)//>代码之间。code>p
  • 实际上在while循环中递增。在检查回文之前,必须删除空格和标点符号。例如,如果使用
    civic?
    ,则它不是回文,因为
    。另一方面,如果使用
    civ ic
    ,则由于空白,它不是回文。1.将所有字母转换为大写或小写。2.删除空白。3.删除标点符号。4.是否检查回文。正则表达式是执行上面提到的Kalana任务的好方法。即使您没有选择在本例中使用Regex,它也是值得学习的。它将迅速解决数量惊人的编程问题,并且在不同语言之间相当一致。无论何时使用regex,我建议在编写程序所需的必要代码之前,在在线regex编辑器上测试您的表达式,因为很容易引入轻微的语法错误。这将节省大量时间。正则表达式很容易学习,即使语法看起来很陌生。我经常使用-甚至有代码生成器。一些学习正则表达式的资源,以及如何在C中使用正则表达式的资源,,,,哇,有些人真的很想帮忙,而不是因为是初学者而被遗忘,或者最后说“改为做这个”。非常感谢你。我肯定会在“未签名的角色扮演”这件事上搜索。@whysoseous我对你的问题投了赞成票。:)但我只能这样做一次。:)我认为你不值得这样的反对票。一些用户否决了你的问题,因为你没有清楚地提到你的问题。我已经理解了你的问题,并对其进行了编辑,以便让每个人都明白。
    while (  p < message + MAX_LEN && ( c = toupper(getchar())) != EOF && c != '\n') {
        if (isalpha(c))
            *p++ = c;
    }
    
    p--;
    
    #include <stdio.h>
    #include <ctype.h> 
    
    #define MAX_LEN 100
    
    int main(void) 
    {
        char message[MAX_LEN];
    
        printf( "Enter a message: " );
    
        char *p = message;
    
        for ( int c; p < message + MAX_LEN &&  ( c = getchar() ) != EOF && c != '\n';   )
        {
            if( isalnum( ( unsigned char )c ) )
            {
                *p++ = toupper( ( unsigned char )c );
            }           
        }
    
        int palindrome = 1;
    
        if ( p != message )
        {
            for ( char *q = message; palindrome && q < --p; ++q )
            {
                palindrome = *q == *p;
            }           
        }
    
        printf( "The entered message is %spalindrome\n", 
                palindrome ? "" : "not " );
    
        return 0;
    }
    
    Enter a message: He lived as a devil, eh?
    The entered message is palindrome
    
    Enter a message: Madam, I am Adam
    The entered message is not palindrome
    
    fgets( message, sizeof( message ), stdin );
    
    if ( fgets( message, sizeof( message ), stdin ) != NULL )
    {
        // check whether the entered string is a palindrome
    }