5-1 字符串的输入输出
题目描述
利用循环结构连续调用 scanf(”%[^\n]”,str) 输入多个字符串,并使用 printf(),将 str 输出。根据 str 的输出结果,观察是否在输入一个字符串后,后续的 scanf(”%[^\n]”,str)中,会将缓冲区中残留的回车换行符读入到 str 中。
提示:利用格式 scanf(”%[^\n]”,str)输入;
注意输入缓冲区的数据残留;如 scanf(”%[^\n]”,str)语句会造成输入缓冲中残留回车换行符。 根据 str 的输出结果理解输入缓冲区残留的问题,并使用相应的解决方案,以消除数据残留所带来的副作用。
输入格式
第一行一个整数 n,代表接下来有多少行字符串需要读取,接下来 n 行为待读取的字符串,字符串中包含空格。
输出格式
输出读入的 n 行字符串
#include <stdio.h>
int main()
{
char str[1000];
int n;scanf("%d",&n);getchar();
for(int i=0;i<n;i++)
{
scanf("%[^\n]",str);getchar();
printf("%s\n",str);
}
}
5-2 复数转换
题目描述
键盘输入一个英文单词,输出其对应的复数形式
规则如下:
(a)以辅音字母 +y 结尾,则将 y 变成 i,再加 es;
(b)以 s,x,ch,sh,o 结尾,加 es;
(c)其它情况,加 s;
注:系统只会提供符合以上变换规律的英文单词
输入格式
一个字符串,为待变换的单词
输出格式
一个字符串,为变换后的单词
#include <stdio.h>
#include <string.h>
int main()
{
char word[100];scanf("%s",word);
int len=strlen(word);char i[]="es";char j[]="s";
if(word[len-1]=='y'&&word[len-2]!='a'&&word[len-2]!='e'&&
word[len-2]!='i'&&word[len-1]!='o'&&word[len-2]!='u'){
word[len-1]='i';strcat(word,i);
}
else if(word[len-1]=='s'||word[len-1]=='x'||
((word[len-2]=='c'||word[len-2]=='s')&&word[len-1]=='h')||word[len-1]=='o'){
strcat(word,i);
}
else strcat(word,j);
printf("%s",word);
}
2022

被折叠的 条评论
为什么被折叠?



