输入一个字符串,输出该字符串的子串。
首先输入一个正整数k,然后是一个字符串s可能包含空格,长度不超过20,k和s之间用一个空格分开。k大于0且小于等于s的长度
在一行中输出字符串s从头开始且长度为k的子串。
输入
10 welcome to acm world
输出
welcome to
#include <stdio.h>
int main() {
int k;
char s[20+1];
sscanf("%d %20[^\n]",&k,s);
s[k]=0;
printf("%s",s);
return 0;
}
供参考:
#include <stdio.h>
int main()
{
int k;
char s[21], * p = s;
scanf("%d", &k);
getchar();
gets(s);
while (k-- && *p)
printf("%c", *p++);
return 0;
}