题目描述
题目描述
对字符串中的所有单词进行倒排。
说明:
1、每个单词是以26个大写或小写英文字母构成;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长20个字母;
样例输入
I am a student
样例输入
student a am I
# include < stdio.h >
# include < string.h >
# define MAX 20
int main ()
{
int txt( char c);
int i , j , n, m, k = 0 ;
int state = 1;
char s[100];
char str[100];
gets(s);
i =strlen ( s ) - 1;
while ( i != 0)
{
for ( j = i ; j >= 0 ; j--)
{
if ( j == 0)
{
state = 0;
break;
}
else
if( txt( s[j]) == 0 )
{
state = 0;
break;
}
else
state = 1;
}
if ( (state == 0 )&& (j != 0))
{
for ( n = j+1; n <= i; n++)
str[k++] = s[n];
str[k++] = ' ';
for ( m = j - 1; m >= 0; m-- )
{
if ( m == 0)
i = 0;
if (txt ( s[m]))
{
if ( m == 0)
j = 0;
i = m;
break;
}
}
}
if ( (j == 0) )
{
for ( n = j; n <= i; n++)
str[k++] = s[n];
str[k] = '\0';
i = 0;
}
}
for ( i = 0; i < k; i++)
printf ( "%c", str[i]);
printf ("\n");
return 0;
}
int txt( char c)
{
if ( (c <= 90 && c >= 65) || (c <=122 && c >= 97) )
return 1;
else
return 0;
}