gpt4 book ai didi

c - 在 C 中将值插入数组

转载 作者:行者123 更新时间:2023-12-04 04:33:22 25 4
gpt4 key购买 nike

希望输入->

"Enter a word: program"
"Enter a postion to know where to start cutting: 2"
"Enter the following quantity to end up: 4"

希望的输出->
ogra

这是我的代码,但我不知道如何将值插入到不同的数组中
#include <stdio.h>
#include <string.h>
int main (){

char word[100], cutword[50];
int on, off, i, j;

printf("Entra a word: ");
gets(word);
printf("Enter a postion to know where to start cutting: ");
scanf("%d", &on);
printf("Enter the following quantity to end up: ");
scanf("%d", &off);

for (i = 0; i<strlen(word); i++){
if (i >= on && i <= off) ¿cutword[]? = word[i];
}

for (j = 0; j<strlen(cutword); j++){
printf("%c", cutword[j]);
}

}

最佳答案

int k = 0;
int l = on + off;
for (i = on; i < l; i++){
cutword[k++] = word[i];
}

for (j = 0; j < k; j++){
printf("%c", cutword[j]);
}

添加一些额外的 if在这些循环之前验证输入并检查数组边界。

关于c - 在 C 中将值插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com