The string in C is really too many places to pay attention to, yesterday in the knowledge to see some of the problems to think carefully, I still do not understand ...
These days began to listen to the University of Zhejiang University Onge teacher Mooc just also talked about the string, probably summed up some ... About the string, I will certainly have a good summary, and so on after reading "C and pointers."
Start by making a wheel of your own, and write a function of your own string (Mystrlen).
#include <stdio.h>#include<stdlib.h>#defineN 80intMystrlen (Const Char*pstr);intMain () {intLen; CharStr[n]; Gets (str); Len=Mystrlen (str); printf ("%d", Len); return 0;}intMystrlen (Const Char*pstr) { Char*qstr=pstr; while(*qstr) {Qstr++; } returnqstr-pstr;}
Well, our Mystrlen has been able to calculate the length of the string, in fact it is exactly the same as the result of the strlen function, so it also means that we don't consider the length of this byte. So what if we write the above program like this?
#include <stdio.h>#include<stdlib.h>#defineN 80intMystrlen (Const Char*pstr);intMain () {intLen; CharStr[n]; Gets (str); Len=Mystrlen (str); printf ("%d", Len); return 0;}intMystrlen (Const Char*pstr) { Char*qstr=pstr; while(*qstr++); returnqstr-pstr;}
The result is more than the length of a character, why is this? We analyze the while statement, in fact, because we qstr this pointer after the reference, although it is determined to be a null character, it has since been added again, this only jumps out of the loop.
Then recreate a mystrcmp function. For strcmp This function we know to compare two strings, the equivalent returns 0, the former large returns 1, The latter large returns-1. But it looks like this. I also checked some information, found that there are two sources of C language, a return value is the same as before, but the other is, equal to return 0, not equal returns the difference of ASCII code of two unequal characters. However, the former has a large return positive value, and the latter will return a negative value.
#include <stdio.h>int mycmp (constchar*s1,constchar*S2) { while (*s1==*s2&&*s1!=') { S1+ +; S2++ ; } return *s1-*s2;}
I found a copy of source code ASCI C.
/*strcmp function*/#include<string.h>int(Strap) (Const Char*SL,Const Char*S2) { /*Compare unsigned char sl[],s2[]*/ for(; *sl==*s2;++sl,++S2)if(*sl==' /') return(0); return(* (* (unsignedchar*) sl<* (unsignedchar*) s2)?1:+1);}
All right, now build the wheel: Write a mystrcpy. The video describes a function prototype for strcpy: Char *strcpy (char *restrict dst,const char*restrict src); here is a new keyword restrict, which is the C99 new keyword, It is mainly said that these two strings can not overlap. Well, the explanation here is not very clear, I will look into the information to explain in detail. Leave a hole.
Char Char* DST,constchar *src) { char*ret=DST; while (*src) { *dst++=*src++; } *dst='+ '; return ret;}
Also we can analyze, while the statement, in fact, *DST=*SRC is the *src assignment to *DST? Is the value of this expression *src? So we can also write this:
while (*dst++=*src++);
Let's take a look at another function, which is actually two:
Char* STRCHR (constchar *s,int c); // indicates that the character is searched from the left of the string Char* STRRCHR (constchar*s,nnt c); // represents the character starting from the right of the character
For example, we are now looking for the location of the "Hello" string where L appears, using this function we can get the result "Llo". What if we need to get the He?
#include <stdio.h>#include <string.h>
int main () { char s[]= hello " ; char *p,*T; char C; P =strchr (S, l ' ); C =*P; *p= " \0
*p=c; return 0 ;}
So I can get the He, why? Because we've got p pointing to the first L by STRCHR this function, and then we change it to nul so does s not become "he"?
In fact, there are functions that can be used to find a string in the string, the sum, also leave a hole.
Recently need to write a lot of math homework, and the weekend also has a four-level exam, it must be to focus on the four level. As for the school race, yesterday also wrote two water problems but, go to sign it. That busy this week, I will hurry to write blog, the Onge of the class notes to write, as well as Peking University, the garlic on the guest there is an ACM problem has not been clear. These are all things to do next week ... The first level of the four.
Defragment the string ...