c语言字符串操作实现

复试前,用字符串相关的操作来练练手。敲一遍总该用些用处。

  1 #include <stdio.h>
  2 #include <assert.h>
  3 #include <malloc.h>
  4 
  5 int strlen(const char *str)
  6 {
  7     assert(str!=NULL);
  8     int len = 0;
  9     while(*str++)
 10         ++len;
 11     return len;
 12 }
 13 
 14 char *strcpy(char *to, const char *from)
 15 {
 16     assert((to!=NULL)&&(from!=NULL));
 17     char *result = to;
 18     while(*to++ = *from++);
 19     return result;
 20 }
 21 
 22 char *strncpy(char *to, const char *from, size_t count)
 23 {
 24     assert((to!=NULL)&&(from!=NULL));
 25     char *result = to;
 26     while (count--)
 27     {
 28         if (*from)
 29             *to++=*from++;
 30         else
 31         {
 32             *to++='\0';
 33             break;
 34         }
 35     }
 36     return result;
 37 }
 38 
 39 void *memset(void *buffer, int c, size_t count)
 40 {
 41     assert(buffer!=NULL);
 42     char *p = (char *)buffer;
 43     while(count--)
 44         *p++ = (char)c;
 45     return buffer;
 46 }
 47 
 48 char *strchr(char *str, int c)
 49 {
 50     assert(str!=NULL);
 51     for (;*str!=(char)c;++str)
 52     {
 53         if(!(*str))
 54             return NULL;
 55     }
 56     return str;
 57 }
 58 
 59 //need to release the block by hand in your outside code
 60 char *strcat(char *strDes, const char *strSrc)
 61 {
 62     assert((strDes!=NULL)&&(strSrc!=NULL));
 63     char *address = strDes;
 64     int len = 0;
 65     while(*strDes)
 66     {
 67         len++;
 68         strDes++;
 69     }
 70     char *strResult = (char *)malloc(len+1+strlen(strSrc));
 71     char *strRtmp = strResult; 
 72     while(*strResult++=*address++);
 73     strResult--;
 74     while(*strResult++=*strSrc++);
 75     return strRtmp;
 76 }
 77 
 78 
 79 char * strncat(char *strDes, const char *strSrc, unsigned int count)
 80 {
 81     assert((strDes!=NULL)&&(strSrc!=NULL));
 82     char *address = strDes;
 83     while(*strDes)
 84         ++strDes;
 85     while(count--&&*strSrc)
 86         *strDes++=*strSrc++;
 87     *strDes = '\0';
 88     return address;
 89 }
 90 
 91 char *strstr(const char *strSrc, const char *str)
 92 {
 93     assert(strSrc!=NULL&&str!=NULL);
 94     const char *s = strSrc;
 95     const char *t = str;
 96     for (;*strSrc;++strSrc)
 97     {
 98         for(s=strSrc,t=str;*t&&*s==*t;++s,++t);
 99         if(!(*t))
100             return (char *)strSrc;
101     }
102     return NULL;
103 }
104 
105 //need to release the block by hand in your outside code
106 char *strdup(char *strSrc)
107 {
108     if (strSrc!=NULL)
109     {
110         char *start = strSrc;
111         int len = 0;
112         while(*strSrc++)
113             len++;
114         char *address =(char *)malloc(len+1);
115         assert(address!=NULL);
116         while((*address++=*start++));
117         return address-(len+1);
118     }
119     return NULL;
120 }
121 
122 void *memcpy(void *to, const void *from, size_t count)
123 {
124     assert((to!=NULL)&&(from!=NULL));
125     void *result = to;
126     char *pto = (char *)to;
127     char *pfrom = (char *)from;
128     while(count--)
129         *pto++=*pfrom++;
130     return result;
131 }
132 
133 void *memmove(void *to, const void *from, size_t count)
134 {
135     assert((to!=NULL)&&(from!=NULL));
136     void *result = to;
137     char *pto = (char *)to;
138     char *pfrom = (char *)from;
139     assert(pto<pfrom||pto>pfrom+count-1);
140     if(pto<pfrom||pto>pfrom+count-1)
141         while(count--)
142             *pto++ = *pfrom++;
143     else
144     {
145         pto = pto+count-1;
146         pfrom = pfrom+count-1;
147         while(count--)
148             *pto-- = *pfrom--;
149     }
150     return result;
151 }
152 
153 
154 int strcmp(const char *s, const char *t)
155 {
156     assert(s!=NULL&&t!=NULL);
157     while(*s&&*t&&*s==*t)
158     {
159         ++s;++t;
160     }
161     return (*s-*t);
162 }
163 
164 
165 int stricmp(const char *dst, const char *src)
166 {
167     assert(dst!=NULL&&src!=NULL);
168     int ch1, ch2;
169     while (*dst&&*src)
170     {
171         if((ch1=(int)*dst)>='A'&&(ch1<='Z'))
172             ch1+=0x20;
173         if((ch2=(int)*src)>='A'&&(ch2<='Z'))
174             ch2+=0x20;
175         if(ch1==ch2)
176         {
177             ++dst;++src;
178         }
179         else break;
180     }
181     return (ch1-ch2);
182 }
183 
184 
185 int strncmp(const char *s, const char *t, unsigned int count)
186 {
187     assert((s!=NULL)&&(t!=NULL));
188     while (*s&&*t&&*s==*t&&count--)
189     {
190         ++s;++t;
191     }
192     return (*s-*t);
193 }
原文地址:https://www.cnblogs.com/xuangong/p/2964328.html