将值插入C中的数组 [英] Insert values into array in C
本文介绍了将值插入C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
祝输入 - >
输入一个单词:计划
输入现在的位置知道从哪里开始削减:2
输入下列数量结束了:4
祝输出 - >
ogra
这是我的code,但我不知道如何插入值到不同的阵列
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
诠释主(){ 炭字[100],cutword [50];
诠释开,关,I,J; 的printf(ENTRA一句话:);
得到(字);
的printf(请输入现在的位置知道从哪里开始削减:);
scanf的(%d个,&放大器;上);
的printf(请输入以下数量结束了:);
scanf函数(%d个,&安培;关闭); 对于(i = 0; I<的strlen(字);我++){
如果(I> = ON&功放;&安培; I< =关)¿cutword []? =字[我]
} 为(J = 0; J<的strlen(cutword); J ++){
的printf(%C,cutword [J]);
}}
解决方案
时int k = 0;
INT L = +上关;
对于(I =开; I<升;我++){
cutword [K +] =字[我]
}为(J = 0; J< k; J ++){
的printf(%C,cutword [J]);
}
把一些额外的如果
来验证输入这些循环之前检查数组边界。
Wished Input ->
"Enter a word: program"
"Enter a postion to know where to start cutting: 2"
"Enter the following quantity to end up: 4"
Wished Output ->
ogra
This is my code, but I don't know how to insert values into a different array
#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]);
}
Put some additional if
to validate the input and check array boundaries before these loops.
这篇关于将值插入C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文