#include
#include<assert.h>
using namespace std;
void reverse(char *pbegin, char *pend ) // 将一句话 词组逆序输出
{ char tmp = 0;
while (pbegin < pend)
{
tmp= *pbegin;
*pbegin = *pend;
*pend = tmp;
*pbegin++;
pend–;
}
}
char reversentence(char *str)
{
assert(str != NULL);
char *pbegin;char *pend;
pbegin = str; pend = str;
while(*pend!=’\0’)
{
pend++;
}
pend–;
reverse(pbegin, pend);
pbegin = str; pend = str;
while (*pbegin !=’\0’)
{if(*pbegin==’ ‘)
{
pend++;pbegin++;
}else if(*pend==’ ‘||*pend==’\0’)
{
reverse(pbegin, --pend);
pbegin = ++pend;
}
else pend++;
}
return str;
}
bool xiantong(char *a) //判断 回文数
{
char *pbegin = a; char *pend = a;
while (*pend != ‘\0’)
{
pend++;
}
pend–;
while (pend != pbegin)
{
if (*pend == *pbegin)
{pend–;
pbegin++;}
else return false;
}
return true;
}
int main()
{
int i = 0;
char a[] = “i am a student”;
cout << reversentence(a);
char a[] = “123331”;
xiantong(a);
if (xiantong(a))
cout << “是”;
else cout << “不是”;
cin >> i;
return 0;
}
初学C语言(5):逆序输出词组 ,判断回文数
最新推荐文章于 2023-11-11 22:34:13 发布