题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <stdio.h>
#include <string.h>
int main() {
int i,j,t,len,count=0,flag;
char str[10001],str_new[10001],test[100];
char *p=str_new;
fgets(str,10001,stdin);
for(i=strlen(str)-1;i>=0;i--)
{
if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
{
str_new[count]=str[i];
flag=0;
count++;
}
else if(flag==0)
{
flag=1;
str_new[count]=' ';
count++;
}
}
len=0;
for(i=0;i<strlen(str_new);i++)
{
if(str_new[i]==' ')
{
t=0;
for(j=i-1;j>=len;j--)
{
printf("%c",str_new[j]);
t++;
}
len=i+1;
printf(" ");
}
else if(i==strlen(str_new)-1)
{
for(j=i;j>=len;j--)
{
printf("%c",str_new[j]);
}
}
}
return 0;
}