将一个字符串中的单词逆序输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void converse(char *str)
{
int len=strlen(str);
char *pStart=str;
char *pEnd=str+len-1;
char tempChar;
if(str!=NULL)
{
while(pStart<pEnd)
{
tempChar=*pStart;
*pStart++=*pEnd;
*pEnd--=tempChar;
}
}
}
int main()
{
char str[]="a string need to converse !";
char seps[]=" ";
char *token;
printf("%s\n",str);
converse(str);
token=strtok(str,seps);
while(token!=NULL)
{
converse(token);
printf("%s\n",token);
token=strtok(NULL,seps);
}
return 0;
}
输出:
!
converse
to
need
string
a