C 程序检查字符串是否为回文 [英] C Program to check if string is Palindrome or not

查看:59
本文介绍了C 程序检查字符串是否为回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 C 程序有些困难!它应该检查字符串是否是回文!它不应该关注非字母字符,因此程序应该将其识别为回文!他像恶魔一样生活,嗯?"这就是我到目前为止所得到的:

#include #include int main(){字符句[39];int 左 = 0;int 右 = 40;printf("请输入信息:");fgets(sentence, 40, stdin);而(1){while(left < right && !(isalpha(sentence[left])))左++;while(right > left && !(isalpha(sentence[right])))正确的 - ;如果(左> =右)休息;别的 {如果(句子[左] != 句子[右]){printf("不是回文");返回0;}左++;正确的 - ;}}printf("回文");返回0;}

它总是在打印:不是回文!即使是一个.

解决方案

我对您的程序进行了一些更改.首先不破坏数组索引,接下来使用字符串长度而不是访问未定义的值,第三次检查相同的大小写字母.

#include #include #include int main(){字符句[200];//提供足够的空间int 左 = 0;正确的;//不假设长度printf("请输入信息:");fgets(句子,句子大小,标准输入);//限制输入right = strlen(句子);//现在获取长度而(1){while(left < right && !(isalpha(sentence[left])))左++;while(right > left && !(isalpha(sentence[right])))正确的 - ;如果(左> =右)休息;别的 {if(toupper(sentence[left]) != toupper(sentence[right])) {//得到相同的情况printf("不是回文\n");返回0;}左++;正确的 - ;}}printf("回文\n");返回0;}

计划会话:

<前>输入一条消息:他像魔鬼一样生活,嗯?回文输入留言:回文不是回文

i have some struggles with my C program! It should check wether a string is a palindrome or not! it should not pay attention to non-alphabetic characters, so the program should recognize this as a palindrome! "He lived as a devil, eh?" That's what i got so far:

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

int main()
{
    char sentence[39];
    int left = 0;
    int right = 40;

    printf("Enter a message: ");
    fgets(sentence, 40, stdin);

    while(1) {

        while(left < right && !(isalpha(sentence[left])))
            left++;
        while(right > left && !(isalpha(sentence[right])))
            right--;

        if(left >= right)
            break;

        else {

            if(sentence[left] != sentence[right]) {
                printf("Not a Palindrome");
                return 0;
            }

            left++;
            right--;
        }
    }

    printf("Palindrome");

    return 0;
}

It's always printing: NOT A PALINDROME! Even if it is one.

解决方案

I have made a few changes to your program. Firstly not breaking the array indexing, next using the string length instead of accessing undefined values, third checking same case letters.

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

int main()
{
    char sentence[200];                             // provide plenty of room
    int left = 0;
    int right;                                      // do not assume the length

    printf("Enter a message: ");
    fgets(sentence, sizeof sentence, stdin);        // limit the input
    right = strlen(sentence);                       // now get the length

    while(1) {
        while(left < right && !(isalpha(sentence[left])))
            left++;
        while(right > left && !(isalpha(sentence[right])))
            right--;
        if(left >= right)
            break;
        else {
            if(toupper(sentence[left]) != toupper(sentence[right])) {  // get case the same
                printf("Not a Palindrome\n");
                return 0;
            }
            left++;
            right--;
        }
    }

    printf("Palindrome\n");
    return 0;
}

Program sessions:

Enter a message: He lived as a devil, eh?
Palindrome

Enter a message: palindrome
Not a Palindrome

这篇关于C 程序检查字符串是否为回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆