HZNUOJ 【C系列6.19】字符串训练之吉祥物读号码

gate

需要用C语言实现读入一行含空格的字符串,又不想一个一个char读入?

scanf在读入字符串时遇到指定字符结束:

scanf("%[^
]s", s); //遇到换行结束
scanf("%[^#,$,%]s", s); //遇到#或$或%结束

不写的话,就是默认遇到换行或空格结束

对于这道题,为了避免读入到上一行末尾的换行符直接结束的情况,需要在前面加一个

scanf("
%[^
]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("
%[^
]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("
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/mogeko/p/15391257.html