//Reverse_string.h
#ifndef REVERSE_STRING_H
#define REVERSE_STRING_H
#include<string> // #include<cstring> will cause error!
string Reverse(string str)
{
string S;
int n=str.size();
while(n>0)
{
n--;
S+=str[n];
}
return S;
}
string Convert(string str)
{
int n=str.size();
while(n>0)
{
n--;
if(str[n]>=65&&str[n]<=90) str[n]+=32; //convert capital letter into small letter,in order to ignore the form
//str[n]=(str[n]>90? str[n]+32:str[n]);
}
return str;
}
string Ignore(string str)
{
int n=str.size();
string s;
for(int i=0;i<n;i++)
{
if((str[n]>=65&&str[n]<=90)||(str[n]>=97&&str[n]<=122))
s+=str[i];
}
return s;
}
#endif
////////////////////////////////
//main():
#include<iostream>
#include<fstream>
#include<vector>
#include <stdio.h>
using namespace std;
#include "Reverse_string.h"
void main()
{
vector<string> vs;
string str;
char* ch="0";
char* filename="Reverse_string.h";
cin>>filename;
/*
ifstream infile(filename.c_str(),ios::in); // "filename.c_str()" instead of "filename",right!
//如果你要用C中的fopen打开文件,那你可以用feof()判断
if(!infile)
exit(1);
*/
FILE* infile=fopen(filename,"r");
while(!feof(infile)){
fgets(ch,1000,infile);
}
int len=sizeof(ch);
for(int i=0;i<len;i++)
{
if(*(ch+i)!=32) //Intervals characters with spaces
str+=*(ch+i);
else
vs.push_back(str);
}
// read from vector and judge:
vector<string>::iterator vi;
for(vi=vs.begin();vi!=vs.end();vi++)
{
string s=Convert(Ignore(str));
if(Reverse(s)==s)
cout<<str<<endl;
else
cout<<"There is no palindrome!"<<endl;
}
}
/*
int len=str.size();
把从文件中提取的长字符串以空格分开,然后分别存入vector中
vector<string> vs;
while(str!=EOF)
{
infile>>str;
vs.push_back(str);
}
*/
评论