c语言在一列数组中,删除一个元素

如题所述

就是将删除元素后面的元素顺序前移一个位置,覆盖掉被删除的元素的值,然后数组中有效数据个数减一完成删除。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-13
最笨的方法把后面的元素向前移动一位。
void delete_item(int a[], int item, int n)
{
int i, j;
i = 0;
while(a[i] != item) i++;
n--;
for(;i<n;i++)a[i]=a[i+1];

}本回答被网友采纳
第2个回答  2018-03-13
设数组大小为M,删掉第N个元素。
int i;
for(i=N;i<M;i++)
a[i-1]=a[i];
a[M-1]=0;
第3个回答  2018-03-13
从要删除的元素开始,将数组后面的元素覆盖前面的元素,就把这个元素删除掉了。覆盖时注意从要删除的元素开始往后遍历覆盖。
第4个回答  2014-12-22
#include <stdio.h>

#include <stdlib.h>
void delchr(char *s, char c)

{
char *str = s;
char *temp, *temp1;
while(*str != '\0')
{
if(*str == c)
{
temp = str;
temp1 = str + 1;
while(*temp1 != '\0')
{
*temp = *temp1;
temp++;
temp1++;
}
*temp = '\0';
continue;
}
str++;
}
}

int main()
{
char str[100];
char c;

gets(str);
scanf("%c", &c);
printf("the old string is:%s\n", str);
printf("delete '%c'\n", c);
delchr(str, c);
printf("end old string is:%s\n", str);
return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网