The implementations of the other answers nay have a hidden error.
Only moving later characters does not solve the problem
Follow suit:
Type a word up to 10 letters: abcdefg
Type another word up to 10 letters: bc
i str1 str1[i] resultado
--------------------------------------
0 abcdefg 'a' não encontrado
1 abcdefg 'b' encontrado, move caracteres str1
2 acdefg 'c' não encontrado, pois agora 'c' está na posição 1
Whenever there are two characters to be removed in sequence, the second will not be removed.
To resolve this, after removing a character (moving the later ones), one must adjust the control variable i
, as well as the string size size_str1
, in both cases (subtracting the removed character).
Do not subtract from size and use criteria <
at the same time
While recovering string size by subtracting a drive (strlen(str1)-1
) you must make the loop using the smaller or equal operator (<=
) and not equal.
As it stands, the last character is not being considered.
In the case of Ideone and maybe in your console, there is a problem related to inputs that come with spaces or line breaks at the end.
However, subtracting a unit from the size does not solve the problem. Ideally use a function that removes these unwanted white characters from the end of the input.
The correct form of the loop
Without subtracting the size, all characters are checked. Decreasing the size size_str1
, save unnecessary iterations. Decreasing i
, avoid skipping characters.
size_str1 = strlen(str1);
size_str2 = strlen(str2);
for(i=0; i<size_str1; i++){
for(j=0; j<size_str2; j++){
if(str1[i] == str2[j]){
for(k=i; k<size_str1; k++) {
str1[k] = str1[k+1];
}
size_str1--;
i--;
}
}
}
We can improve this further
There are routines ready to move memory blocks.
The simplest. It simply copies a chunk of memory of a certain size to another chunk of target memory.
Example of the loop using this function:
size_str1 = strlen(str1);
size_str2 = strlen(str2);
for(i=0; i<size_str1; i++){
for(j=0; j<size_str2; j++){
if(str1[i] == str2[j]){
memcpy(str1, str1, size_str1 - i);
size_str1--;
i--;
}
}
}
This function does practically the same thing as the previous one, but was checks if there will be memory overlap.
Normally this is not necessary. For example, in the above implementations we move the characters from the later position to the previous one in sequence. This way, there is no risk of losing memory.
But try to do the opposite. If you try to make a loop to move the previous character to the later one, then you will have problems. This is because when you move a character to "forward", you have just lost the next character. In this case the function memcpy
would not have the expected result.
According to the documentation, some implementations of memmove
check whether the destination pointer is in a previous or a later position in the memory relative to the source pointer. Depending on the case, it loops backwards to avoid the problem mentioned above. Other implementations can copy in an intermediate area of memory.
What is a null character? Maybe what you want is to delete that character and pull to the "left" the rest of the vector, that’s it?
– Math
That’s right.......
– Meeeefiu
Another approach is to create a third array only with characters that are not found in the second string.
– Vandemberg Lima