c-moodle/homework/11976.c

34 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <string.h>
// 定义函数copykn返回类型为char类型的指针接受四个参数
char* copykn(char* s1, char* s2, int n, int k) {
// 定义一个变量i初始化为n
int i = n;
// 在s1中查找从位置n开始的k个字符
// 当i小于等于s1字符串的长度且i位置上不是'\0'并且还没有复制k个字符时执行以下操作
while (i <= strlen(s1) && s1[i] != '\0' && i < n + k) {
// 将s1的第i个字符复制到s2数组中的对应位置
s2[i - n] = s1[i];
// i自增1
i++;
}
// 返回s2的首地址
return s2;
}
// 主函数
int main() {
// 定义字符串s1和s2分别用于存储输入的字符串和输出的字符串
char s1[10005], s2[10005];
// 定义整型变量n和k分别表示从第n个字符开始复制最多k个字符
int n, k;
// 从键盘输入字符串s1
gets(s1);
// 从键盘输入整型变量n和k
scanf("%d %d", &n, &k);
// 调用函数copykn将从s1的第n个字符开始的最多k个字符复制到s2中并将s2的首地址输出
puts(copykn(s1, s2, n - 1, k)); // 注意题目中的n是从1开始计数的而在字符串中我们是从0开始计数的所以要将n减去1
// 程序结束
return 0;
}