天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

回文字符串

Posted on 2012-08-18 15:20 hoshelly 阅读(926) 评论(0)  编辑 收藏 引用 所属分类: Programming
编写一程序,检查一给定字符串是否是回文的程序(順读和倒读都是一样的字符串),不考虑空格。例如,对于字符串 if i had a hifi ,你的程序应该报告成功,否则打印失败。

代码测试通过:

#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
    char a[N];
    int i,n,m;
    printf("Input the string: ");
    gets(a);
    m=strlen(a);
    n=strlen(a)/2;
    for(i=0;i<n;i++,m--)
    {
        if(a[i] == ' ')
        {
            i++;
        }
        if(a[m-1] == ' ')
        {
            m--;
        }

        if(a[i] != a[m-1])
            break;
    }
    if( i == n)
        printf("succeed!\n");
    else
        printf("No\n");
     
    return 0;
    
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理