[英]How to “delete” every element in array by value in C
I have been trying to solve this problem for about 5 days..can't find any solution please send help. 我已尝试解决此问题约5天。.找不到任何解决方案,请发送帮助。 I am supposed to implement a function to "delete" every element in an array by value.
我应该实现一个函数,以按值“删除”数组中的每个元素。 Let's say my array is "Hello" and I want to delete every "l".
假设我的数组是“ Hello”,我想删除每个“ l”。 So far I can only delete l once.
到目前为止,我只能删除一次。 By the way keep in mind I am not allowed to use pointers for this function...(we haven't learned that yet in my school) Here's my code:
顺便说一下,请记住,我不允许对此函数使用指针...(我们在学校还没有学到)这是我的代码:
#include <stdio.h>
#include <string.h>
void strdel(char array[], char c);
int main(void)
{
char source[40];
printf("\nStrdel test: ");
strcpy(source, "Hello");
printf("\nsource = %s", source);
strdel(source, 'l');
printf("\nStrdel: new source = %s", source);
return 0;
}
void strdel(char array[], char c)
{
int string_lenght;
int i;
for (string_lenght = 0; array[string_lenght] != '\0'; string_lenght++) {}
for (i = 0; i < string_lenght; i++) {
if (array[i] == c) {
for (i = i; array[i] != '\0'; ++i)
array[i] = array[i + 1];
}
}
}
Simple use 2 indexes, one for reading and one for writing. 简单使用2个索引,一个用于读取,一个用于写入。 @Carl Norum
@卡尔·诺鲁姆
void strdel(char array[], char c) {
int read_index = 0;
int write_index = 0;
while (array[read_index] != '\0') {
if (array[read_index] != c) {
array[write_index] = array[read_index];
write_index++; // Only advance write_index when a character is copied
}
read_index++; // Always advance read_index
}
array[write_index] = '\0';
}
The has O(n) performance, much faster than using nested for()
loops which is O(n*n). 具有O(n)性能,比使用嵌套的
for()
循环O(n * n)快得多。
Details: 细节:
OP: By the way keep in mind I am not allowed to use pointers for this function.
OP:顺便提醒一下,我不允许对此函数使用指针。
Note that array
in void strdel(char array[], char c)
is a pointer, even though it might look like an array. 请注意,即使看起来像
array
, void strdel(char array[], char c)
中的void strdel(char array[], char c)
也是一个指针。
int
for array indexing is OK for learner and much code, yet better to use size_t
. 数组索引的
int
对于学习者和很多代码来说都可以,但是最好使用size_t
。 int
may lack the range needed. int
可能缺少所需的范围。 Type size_t
is an unsigned type that is neither too narrow nor too wide for array indexing needs. size_t
类型是一个无符号类型,对于数组索引的需要,它既不会太窄也不会太宽。 This becomes important for very long strings . 这对于很长的琴弦很重要。
Your problem is related to using the variable i
in both loops. 您的问题与在两个循环中使用变量
i
有关。 So once the inner loop is executed, outer loop will terminate right after. 因此,一旦执行了内循环,外循环将立即终止。
Use another variable for the inner loop. 将另一个变量用于内部循环。
void strdel(char array[], char c)
{
int string_lenght;
int i, j;
for (string_lenght = 0; array[string_lenght] != '\0'; string_lenght++) {}
for (i = 0; i < string_lenght; i++) {
if (array[i] == c) {
for (j = i; array[j] != '\0'; ++j) // Use variable j instead of i
array[j] = array[j + 1];
--i; // Decrement i to "stay" at the same index
--string_lenght; // As one character were just removed
}
}
}
The above shows how to make OPs approach work. 上面显示了如何使OP方法起作用。 For a better solution see the answer from @chux : https://stackoverflow.com/a/53487767/4386427
要获得更好的解决方案,请参见@chux的答案: https ://stackoverflow.com/a/53487767/4386427
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.