Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Please provide detailed explanation of each step #include char *a="you"; char b[
Please provide detailed explanation of each step #include char *a="you"; char b[

The output should be you to China!

The function of this program is to find whether there is a "you" string in the character array b. If so, output the first you and subsequent characters in b; otherwise, do not output.

The meaning of the integer variables i and j will not be repeated here.

The program first defines a character pointer a, pointing to the "you" string. If a character pointer points to a string, the pointer points to the first address of the string. In this question, a currently points to 'y', and after a++, p points to 'o'.

A character array b is then defined, which will not be described in detail.

The detailed explanation is as follows:

#include

char *a="you";

char b[ ]="welcome you to China!";

int main()

{

int i,j=0; char*p;

for(i=0;b[i]!='\0';i++){ //Use a for loop to traverse the array b

if(*a==b[i] ) //Determine whether the character b[i] in the currently traversed b is equal to *a (i.e. 'y')

{

p=a; //Copy pointer a to p, p is equivalent to a copy of a. This is done to ensure that the a pointer remains unchanged

/*The following loop body function: starting from the current character b[i] of the character array b and the string "you" Make a comparison. If the next three characters in b are the same as "you", the "p++" statement will be executed three times, at this time *p = '\0'; otherwise, the break statement will be executed during a certain loop, jumping out of the current loop and continuing to the next loop. One layer of circulation. */

for(j=i;*p!='\0';j++){

if(*p!=b[j] )

< p> break;

p++;

}

if (*p =='\0') //if *p =='\0' , that is, the match is successful and the current loop is jumped out.

break;

}

}

printf("%s",&b[i]);//Output b The string starting from b[i].

return 0;

}