From 3fc5a99e37814a33c9da12acd6bf91c721bf8bdc Mon Sep 17 00:00:00 2001 From: smallkun Date: Fri, 14 Mar 2025 12:14:58 +0800 Subject: [PATCH] Auto commit --- 2208/C语言/源码/字符串-3.c | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2208/C语言/源码/字符串-3.c diff --git a/2208/C语言/源码/字符串-3.c b/2208/C语言/源码/字符串-3.c new file mode 100644 index 0000000..c1af17c --- /dev/null +++ b/2208/C语言/源码/字符串-3.c @@ -0,0 +1,41 @@ +#include +#include + +int main() { + char str1[200], str2[100]; + int i, j, index; + + printf("ַ:"); + fgets(str1, sizeof(str1), stdin); + str1[strcspn(str1, "\n")] = '\0'; // ȥз + + printf("Ҫַ:"); + fgets(str2, sizeof(str2), stdin); + str2[strcspn(str2, "\n")] = '\0'; // ȥз + + printf("Ҫλ:"); + scanf("%d", &index); + + int len1 = strlen(str1); + int len2 = strlen(str2); + + // λǷЧ + if (index < 0 || index > len1) { + printf("λЧ\n"); + return 1; + } + + // ƶַڳռ + for (i = len1; i >= index; i--) { + str1[i + len2] = str1[i]; + } + + // str2 + for (j = 0; j < len2; j++) { + str1[index + j] = str2[j]; + } + + printf("ַ: %s\n", str1); + + return 0; +}