找出文件中的回文

/*--------------------------------------------------------*/
/* This program looks at a character string and determines*/
/* if the string is a palindrome                          */

#include <fstream>
#include <cstring>
#include <cctype>
#include <iostream>
using namespace std;

bool is_palindrome(char string []);

const int BUFFERSIZE = 180;

void main(){
char buffer[BUFFERSIZE],new_string[BUFFERSIZE],filename[50];
int    count;
ifstream palindromes;

cout<<"Enter the name of the input file: ";
cin >>filename;
palindromes.open(filename);
if (palindromes.fail()) {
cout <<"error opening file" << filename << endl;
}
else{
//Input first line from file,process file
palindromes.getline(buffer,BUFFERSIZE);

//While not end of file,process string.
while (!palindromes.eof()) {
count = 0;
for(int k=0; k<strlen(buffer);k++){
if (isalnum(buffer[k])) {
new_string[count] = buffer[k];
count++;
}
}
//Assign null character to end of new_string.
new_string[count]='\0';

cout << buffer;
if (is_palindrome(new_string)) {
cout << "\tthis is a palindrome.\n";
}
else{
cout << "\tThis is not a palindrome.\n";
}
//Input next string.
palindromes.getline(buffer,BUFFERSIZE);
}
}
}

/*This function returns a value of true if string is a palindrome.*/
bool is_palindrome(char string []){
bool is_pal=true;
int begin(0),end;

end = strlen(string)-1;
while (is_pal&&begin<=end) {
if (string[begin]!=string[end]) {
is_pal = false;
}
begin++;
end--;
}
return(is_pal);
}

/*----------------------------------------------------------------*/

转载于:https://www.cnblogs.com/aquar/archive/2009/08/12/3451534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值