Home>

As the title says.
For example, if you enter 123ABCxyz123, 123ABC123 will be displayed.

Error message

You can compile itself, but if you enter 123ABCxyz123, 123ABCxyz123 is displayed as it is.

Applicable source code include "stdio.h" include "stdlib.h"

void Function (char [32]);

// int _tmain (int argc, _TCHAR * argv [])
int main ()
{
char buff [32];

printf ("Enter text \ n");
gets_s (buff, 31);
Function (buff);
printf (buff);
return 0;
}

void Function (char text [32])
{
int i, j;

for (i = 0, j = 0;text [i]! = '\ 0';i ++)
{
if (text [i]! = 'xyz')
{
text [j] = text [i];
j ++;
}
}
text [j] = '\ 0';

return;

I think you should recognize each xyz one by one, but I don't know how.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

c
  • Answer # 1

    Select the code part and pressbefore pressing the [Ask a question] button.

      

    I think you should recognize each xyz one by one, but I don't know how.

    # include
    #include
    void Function (char []);
    int main (void)
    {
        char buff [32];
        printf ("Enter characters \ n");
        gets_s (buff, 31);
        Function (buff);
        printf ("% s \ n", buff);
        return 0;
    }
    void Function (char text [])
    {
        int i, j;
        for (i = 0, j = 0;text [i]! = '\ 0';i ++) {
            if (text [i] == 'x'&&text [i + 1] == 'y'&&text [i + 2] == 'z') {
                i + = 2;
            }
            else {
                text [j] = text [i];
                j ++;
            }
        }
        text [j] = '\ 0';
    }


    Where can I understand this code?

    Addition
    If you enter 123ABCxyz123, "123ABCxyz123" is entered in the main buf.
    Since the text of Function refers to buf of main,
    text [0] = '1' text [1] = '2' text [2] = '3'
    text [3] = 'A' text [4] = 'B' text [5] = 'C'
    text [6] = 'x' text [7] = 'y' text [8] = 'z'
    text [9] = '1' text [10] = '2' text [11] = '3' text [12] = '\ 0'
    It becomes. There is no place that contains 'xyz'.

    Contains three letters of 'x', 'y', 'z' side by side
    You have to find text [6], text [7], text [8].

    text [i] == 'x' and text [i + 1] == 'y' and text [i + 2] == 'z'
    Isn't it natural to check the conditions?
    text [i] == 'x' or text [i + 1] == 'y' or text [i + 2] == 'z'
    It's not a condition. Don't you know the words "and" or "or"?
    "Katsu" is&&, and "or" is ||, so the idea of ​​using&&comes out.

    And the meaning of the&&operator,
    If the left operand is true, evaluate the right operand and return the result as true
    If the left operand is false, the right operand is not evaluated and the operation result is false.

    If the condition of if is true, "xyz" was found, so next time
    You must continue searching from text [9].
    I is now 6. I want to advance three, but because there is i ++ in the for statement
    Here, if you advance two, you advance three.

    else means that if condition is false, "xyz" was not found
    Run text [j] = text [i] to pad the characters and advance j.

  • Answer # 2

      

    I think you should recognize each xyz one by one, but I don't know how.

    The quickest way is to use library functions likestrstr ()(man).