PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1009. 说反话
2018-03-10 10:46
561 查看
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
int main()
{
char str[81];
char arr[100][100];
int c1=0,c2=0;
int i;
gets(str);
for(i=0;str[i]!='\0';i++)//若单词中间只有一个空格
{
arr[c1][c2++]=str[i];
if(str[i+1]==' ')
{
c1++;
c2=0;
i++;
}
}
// for(i=0;str[i]!='\0';i++)//去除相邻单词中间多个空格的算法
// {
// arr[c1][c2++]=str[i];
// while(str[i+1]==' ')
// {
// i++;
// if(str[i+1]!=' ')
// {
// c1++;
// c2=0;
// break;
// }
// }
// }
for(i=c1;i>=0;i--)
{
printf("%s%c",arr[i],i?' ':'\0');
}
return 0;
}
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come输出样例:
Come I Here World HelloC语言代码:#include<stdio.h>
int main()
{
char str[81];
char arr[100][100];
int c1=0,c2=0;
int i;
gets(str);
for(i=0;str[i]!='\0';i++)//若单词中间只有一个空格
{
arr[c1][c2++]=str[i];
if(str[i+1]==' ')
{
c1++;
c2=0;
i++;
}
}
// for(i=0;str[i]!='\0';i++)//去除相邻单词中间多个空格的算法
// {
// arr[c1][c2++]=str[i];
// while(str[i+1]==' ')
// {
// i++;
// if(str[i+1]!=' ')
// {
// c1++;
// c2=0;
// break;
// }
// }
// }
for(i=c1;i>=0;i--)
{
printf("%s%c",arr[i],i?' ':'\0');
}
return 0;
}
相关文章推荐
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1006. 换个格式输出整数
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1007. 素数对猜想
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1008. 数组元素循环右移问题
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1011. A+B和C
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1010. 一元多项式求导
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1012. 数字分类
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1005. 继续(3n+1)猜想
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1013. 数素数
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1014. 福尔摩斯的约会
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1001. 害死人不偿命的(3n+1)猜想
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1003. 我要通过!
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1002. 写出这个数
- PAT (Basic Level) Practise (中文) 1009说反话 (20)
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文) 1009.说反话
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)- 1009. 说反话 (20)
- PAT乙级真题及训练集(22)--1009. 说反话 (20)