#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char s[1001];
char str[1001][1001];
int j=0;
while(scanf("%s",s)==1)
{
for (int i = 0; i < strlen(s); i++)
{
if(islower(s[i]))
s[i]=toupper(s[i]);
else if(isupper(s[i]))
s[i]=tolower(s[i]);
}
strcpy(str[j],s);
j++;
}
for (int x = j-1; x>=0; x--)
{
printf("%s ",str[x]);
}
return 0;
}
for this one, I need to ctrl+z twice and enter to get the answer; but for the one below, I just need an enter to get expected anser.
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
int flag = 1; // 使用 1 表示第一个单词
while (scanf("%s", s) == 1) {
if (flag) {
flag = 0;
printf("%lu", strlen(s));
} else {
printf(",%lu", strlen(s));
}
}
return 0;
}
I have no idea when I can use this kind of input way. And when it comes to word problem, I think it's easier than fgets, so I do not want to give it up.
%sdiscards leading whitespace and scans non-whitespace. It can't detect a newline.One solution would be to use a
.to signal the end of input. Usestrchrto check for a..Another solution would be to use a specific string to signal the end of input.
-end-for example. Usestrcmpto check for the string.strcmpreturns0when the comparison is an exact match.There are other solutions.