some basic program you we can study

 1 strcat

char* strcat(char *s, const char *append) { char *save = s; for (; *s; ++s); while ((*s++ = *append++) != '/0'); return(save); }

2 strlcat

/* * Appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters * will be copied. Always NUL terminates (unless siz <= strlen(dst)). * Returns strlen(src) + MIN(siz, strlen(initial dst)). * If retval >= siz, truncation occurred. */ size_t strlcat(char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; size_t dlen; /* Find the end of dst and adjust bytes left but don't go past end */ while (n-- != 0 && *d != '/0') d++; dlen = d - dst; n = siz - dlen; if (n == 0) return(dlen + strlen(s)); while (*s != '/0') { if (n != 1) { *d++ = *s; n--; } s++; } *d = '/0'; return(dlen + (s - src)); /* count does not include NUL */ }

3 strncat

char * strncat(char *dst, const char *src, size_t n) { if (n != 0) { char *d = dst; const char *s = src; while (*d != 0) d++; do { if ((*d = *s++) == 0) break; d++; } while (--n != 0); *d = 0; } return (dst); }

4 strcmp

int strcmp(const char *s1, const char *s2) { while (*s1 == *s2++) if (*s1++ == 0) return (0); return (*(unsigned char *)s1 - *(unsigned char *)--s2); }

5 strncmp

int strncmp(const char *s1, const char *s2, size_t n) { if (n == 0) return (0); do { if (*s1 != *s2++) return (*(unsigned char *)s1 - *(unsigned char *)--s2); if (*s1++ == 0) break; } while (--n != 0); return (0); }

 

6 strcpy

char * strcpy(char *to, const char *from) { char *save = to; for (; (*to = *from) != '/0'; ++from, ++to); return(save); }

7 strlcpy

/* * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ size_t strlcpy(char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ if (n != 0) { while (--n != 0) { if ((*d++ = *s++) == '/0') break; } } /* Not enough room in dst, add NUL and traverse rest of src */ if (n == 0) { if (siz != 0) *d = '/0'; /* NUL-terminate dst */ while (*s++) ; } return(s - src - 1); /* count does not include NUL */ }

8 strncpy

char * strncpy(char *dst, const char *src, size_t n) { if (n != 0) { char *d = dst; const char *s = src; do { if ((*d++ = *s++) == 0) { /* NUL pad the remaining n-1 bytes */ while (--n != 0) *d++ = 0; break; } } while (--n != 0); } return (dst); }

9 strstr

char * strstr(const char *s, const char *find) { char c, sc; size_t len; if ((c = *find++) != 0) { len = strlen(find); do { do { if ((sc = *s++) == 0) return (NULL); } while (sc != c); } while (strncmp(s, find, len) != 0); s--; } return ((char *)s); }

10 strtok

char * strtok(char *s, const char *delim) { static char *last; return strtok_r(s, delim, &last); } char * strtok_r(char *s, const char *delim, char **last) { char *spanp; int c, sc; char *tok; if (s == NULL && (s = *last) == NULL) return (NULL); /* * Skip (span) leading delimiters (s += strspn(s, delim), sort of). */ cont: c = *s++; for (spanp = (char *)delim; (sc = *spanp++) != 0;) { if (c == sc) goto cont; } if (c == 0) { /* no non-delimiter characters */ *last = NULL; return (NULL); } tok = s - 1; /* * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). * Note that delim must have one NUL; we stop if we see that, too. */ for (;;) { c = *s++; spanp = (char *)delim; do { if ((sc = *spanp++) == c) { if (c == 0) s = NULL; else s[-1] = 0; *last = s; return (tok); } } while (sc != 0); } /* NOTREACHED */ }

 

11 strntou

static inline int digitval(int ch) { unsigned d; d = (unsigned)(ch - '0'); if (d < 10) return (int)d; d = (unsigned)(ch - 'a'); if (d < 6) return (int)(d+10); d = (unsigned)(ch - 'A'); if (d < 6) return (int)(d+10); return -1; } uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { const unsigned char* p = nptr; const unsigned char* end = p + n; int minus = 0; uintmax_t v = 0; int d; /* skip leading space */ while (p < end && isspace(*p)) p++; /* Single optional + or - */ if (p < end) { char c = p[0]; if ( c == '-' || c == '+' ) { minus = (c == '-'); p++; } } if ( base == 0 ) { if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { p += 2; base = 16; } else if ( p+1 < end && p[0] == '0' ) { p += 1; base = 8; } else { base = 10; } } else if ( base == 16 ) { if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { p += 2; } } while ( p < end && (d = digitval(*p)) >= 0 && d < base ) { v = v*base + d; p += 1; } if ( endptr ) *endptr = (char *)p; return minus ? -v : v; }

12 atod

double _atod(char* s) { char *p=s; int sum=0; int base; double frag=0.0; int sign=1; int cnt=1; while(*p==' ') p++; if(*p=='-') sign=-1,p++; while(*p!='/0'&&*p!='.') { sum*=10; base=*p-'0'; if(base<0||base>9) return 0; sum+=base; p++; } if(*p=='.') { p++; while(*p!='/0') { cnt*=10; base=*p-'0'; if(base<0||base>9) return 0; frag+=base*1.0/cnt; p++; } } return sign*(sum+frag); }

 

 

13 index(str1,str2)非KMP实现

 int index(const char* s1,const char* s2) { const char* p1=s1; const char* p2=s2; int i=0; while(*p1!='/0') { while(*p2!='/0') { if(*p1!=*p2) { p2=s2,i=0; break; } else p1++,p2++,i++; if(*p2=='/0') return p1-i-s1; } p1=p1-i+1; } return -1; }

 

14 KMP字符串匹配算法

void get_next(string& s,int a[],int n) { a[0]=0; int k=0; for(int i=1;i<n;i++) { while(k>0 && s[k]!=s[i]) k=a[k-1]; if(s[k]==s[i]) k++; a[i]=k; } } void kmp_match(string& s1,string& s2,int a[]) { int n=s1.length(); int m=s2.length(); int k=0; for(int i=0;i<n;i++) { while(k>0 && s1[i]!=s2[k]) k=a[k-1]; if(s1[i]==s2[k]) k++; cout<<"i="<<i<<" k="<<k<<endl; if(k==m) { cout<<"pattern occurs with shift "<<(i-m+1)<<endl; k=a[k-1]; } } } int main() { string s1="abcaabcacb" ; string s2="abc"; int a[4]; get_next(s2,a,s2.length()); kmp_match(s1,s2,a); }

 

 

 

 

你可能感兴趣的:(c,String,null,basic,include,DST)