描述:
有一段以“”结尾的字符串,该字符串中包括若个单词,单词之间由空格隔开,请求出这段字符串中所包含的回文单词,输出这些单词,中间用空格隔开。
输入:
I AM SURE THE DEED IS ON THE LEVEL MADAM ?
输出:
I DEED LEVEL MADAM
代码:
#include<iostream>
#include<string>
using namespace std;
void Get(int count,char mark[])
{
int j,k,x;
for(j=0,k=count-1;j<count/2;j++,k--)
{
if(mark[j]!=mark[k])
{
count=0;
break;
}
}
if(count!=0)
{
for(x=0;x<count;x++)
{
cout<<mark[x];
}
cout<<" ";
}
}
int main()
{
char str1[1000]="",mark[1000]="";
string len="";
int i=0,count=0,n=0,m=0;
//gets(str1);
scanf("%c",&str1[m]);
while(str1[m]!='*')
{
m++;
scanf("%c",&str1[m]);
}
i=0;
n=strlen(str1);
while(i<n)
{
count=0;
while(1)
{
if(str1[i]==' '||str1[i]=='*'||str1[i]=='?'||str1[i]=='.')
{
i++;
break;
}
mark[count]=str1[i];
i++;
count++;
}
Get(count,mark);
}
return 0;
}
简单小结:
1.cin不接受空格,TAB等键的输入,遇到这些键,字符串会终止,而gets()则接受连续的输入,包括空格,TAB;
2.关于循环的数组下标起始位置的具体设置;
参考链接:https://blog.csdn.net/danny_2016/article/details/78873402