How to remove garbage input from line C?

I am trying 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 string c - it receives most of them, but sometimes leaves some of them.

Input Example:

 123 123 

An example of the output of an array after changing it:

 123123 

However, in some inputs it leaves spaces ...

Input Example:

 123 123 

Output Example:

 123 123 

What can I do to fix this? I feel that the solution is right under my nose, but I cannot find it.

+6
source share
4 answers

This is a classic delete issue along the way: after the for loop, you need to reduce i , otherwise you will skip the next character.

However, a nested for loop is not needed: you can do all this with a single loop, supporting separate read and write indices. When you see a valid character, move it from the reading field to the recording location and increase both indices. When you see an invalid character, increase the read index without moving the character. At the end of the loop, complete the line and you're done!

 int r, w; // r for "read", w for "write" for(r=0, w=0; r != length ; r++) { // Your condition is inverted if(string[r] == '+' || string[r] == '-' || string[r] == '*' || string[r] == '/' || (isdigit(string[r]))) { // No loop inside string[w++] = string[r]; } } // Add null terminator at the end string[w] = '\0'; 
+10
source

The problem is that you are not decreasing the value of i after deleting the character.

Explain it better. If you find a character that is not one of the characters you want, and you delete it, then all other characters will move one index back. And then, you increase i , because of which you will not check the correctness of the first character that you returned in this iteration, thereby, this character is skipped.

Let's look at your string . After entering, it becomes

 1 2 3 1 2 3 ^ ^ ^ ^ ^ ^ ^ ^ ^ 0 1 2 3 4 5 6 7 8 // these are the indeces 

Now let's skip a few iterations and move on to the part where the space with index 3 is deleted. After deleting it, your line will look like

 1 2 3 1 2 3 3 ^ ^ ^ ^ ^ ^ ^ ^ ^ 0 1 2 3 4 5 6 7 8 

But then you go to the next index i , that is, to i = 4 , and the space in i = 3 remains as it is. And after this iteration, it becomes

 1 2 3 1 2 3 3 3 ^ ^ ^ ^ ^ ^ ^ ^ ^ 0 1 2 3 4 5 6 7 8 

As you can see, the space remains there. This is what causes the problem.

So, you need to reduce i and length so that all characters are marked.

You have to do

 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]; } i--; length--; } } string[i]='\0'; printf("%s",string); } 

I also added

  string[i]='\0'; 

So you can finish the array with the desired length.

+3
source

When you remove a character from an array, you decrease the length, so you need to reduce the length of the variable by one, as well as the index by one.

Also remember that lines in c end with zero, so you also need to copy the trailing null character, otherwise you will skip the next character.

If you make these changes, your function will look like this:

 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 + 1; j++){ string[j] = string[j+1]; } length--; i--; } } } 
+2
source
 there are a few problems with the code. 1) the string actually gets shorter when ever a character is removed. 2) when a character is removed, i needs to point to the new current character, not the next character. The 'for' statement will increment 'i' the code needs to decrement 'i'. 3) the new string array is unused. suggest either copying the characters to keep to successive positions in the new string array or remove the new string array from the code. suggest compiling with all warnings enabled so the compiler can tell you about problems in the code As it is, that unused new string array is causing the compiler to raise a warning. for several reasons, the warning about the unused variable needs to be fixed. 4) suggest having the for loop check for current char != '\0' so no need to call strlen(), so no need to check for length, so no need to continually adjust the length 
0
source

Source: https://habr.com/ru/post/984837/


All Articles