I am attempting to write a function that will remove all characters from an array, except for '+'
, '-'
, '*'
, '/'
, and numbers. This is the code I came up with:
void eliminateJunk(char string[MAX]){
int i,j;
char stringOut[MAX];
int length = strlen(string) - 1;
for(i=0; i <= length; i++){
if(string[i] != '+'
&& string[i] != '-'
&& string[i] != '*'
&& string[i] != '/'
&& !(isdigit(string[i]))){
for(j=i; j < length; j++){
string[j] = string[j+1];
}
}
}
}
However, the function does not always remove all garbage characters from the c string - it gets most of them, but occasionally leaves some behind.
Example input:
123 123
Example output of array, after it has been modified:
123123
However, in some inputs, it leaves spaces...
Example input:
123 123
Example output:
123 123
What can I do to fix this? I feel like the solution is right under my nose, but I can't seem to find it.
while (string[r])
. This would avoid to callstrlen
. – Unstained