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


gate

需要用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;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM