Home > Back-end >  Answer: the program looking for character why is wrong?
Answer: the program looking for character why is wrong?

Time:10-28

This program is
Used to retrieve the string finally specify the location of the content of
If not found it outputs - 1
Compile a success

Clearly lost in the two arrays are the same ah can also output - 1
What is wrong?

CodePudding user response:

K=sizeof (r) - 2; - 2 h=sizeof (b) whether you print f k and h as well as you think,

CodePudding user response:

Preach the refs array will automatically become a pointer (whether you write the char [] or a char a [4]), sizeof (r) and sizeof (b) return is 8, namely the sizeof (char *), can pass two extra length, or the sizeof (r) - 2 for strlen (r) - 1, in string. The strlen h,
And if you (p & lt; 0 & amp; & J & gt; 0) should be changed to the if (p & lt; 0 & amp; & J & gt;=1), j & gt; 0 and return j + 1 return values must be & gt; 1, a see will know that wrong,
In the first for the front and the if (k & gt;=h) to prevent p & gt;=0 and j & lt; 0, do not add these can also be used normally because an array returns 0 XCC (204), generally not possible this value in the string, but with better

The complete code
 
#include
#include
TIAC (char [r], char [] b) {
Int k=(int) strlen (r) - 1, h=strlen (int), (b) - 1 p, j.
If (k & gt; H)={
for (; K & gt;=0; K -) {
For (p=h, j=k; P & gt;=0 & amp; & R [j] [p]==b; J, p -- -);
If (p & lt; 0 & amp; & J & gt;=1) return j + 1;
}
}
The return - 1;
}
The main () {
Char [] y="qwe", g []="qwe";

Printf (" % d ", TIAC (y, g));

}

CodePudding user response:

Char * STRSTR (const char * haystack, const char *) needle in the haystack string search string for the first time the location of the needle, do not include the terminator '\ 0',
Cycle, the last one is the position that you want to,
  • Related