需要用C語言實現讀入一行含空格的字符串,又不想一個一個char讀入?
scanf在讀入字符串時遇到指定字符結束:
scanf("%[^\n]s", s); //遇到換行結束
scanf("%[^#,$,%]s", s); //遇到#或$或%結束
不寫的話,就是默認遇到換行或空格結束
對於這道題,為了避免讀入到上一行末尾的換行符直接結束的情況,需要在前面加一個\n
scanf("\n%[^\n]s", s);
代碼如下
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MogeKo qwq
int t, len;
char s[50];
int main() {
scanf("%d", &t);
while (t--) {
scanf("\n%[^\n]s", s);
len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] == '0') printf("zero ");
if (s[i] == '1') printf("one ");
if (s[i] == '2') printf("two ");
if (s[i] == '3') printf("three ");
if (s[i] == '4') printf("four ");
if (s[i] == '5') printf("five ");
if (s[i] == '6') printf("six ");
if (s[i] == '7') printf("seven ");
if (s[i] == '8') printf("eight ");
if (s[i] == '9') printf("nine ");
}
printf("\n");
}
return 0;
}