用C++实现对回文的判断

来源:互联网 发布:sql server端口 编辑:程序博客网 时间:2024/05/01 23:16

2009-03-30 21:39:44

这是一个课后题目,自己小编了一下,能实现基本的判断是否是回文字符串,但是还很不完善以后再改进吧 ,呵呵

 

// a.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream.h>
#include <stdio.h>
#include <string.h>

void main()
{
int k,i=0;
char b,a[20];


while((b=getchar())!='0')//输入的字符串以0结束
{
a[i]=b;
i++;
}
if (strlen(a) <= 0)
{
return;
}
for ( k = 0; k< (int)(i/2);k++)
{
if (a[k] != a[i-k-1])
 
{  //cout<<a[i-k-1];
  cout<<"该字符串不是回文!"<<endl;
        break;
 }
 else
  //cout<<k;
 cout<<"该字符串是回文!";break;
 getchar();
}
}