常用的字符串庫函數的內部實現,截自linux內核中的lib/string.c文件,絕對標準的程序,供大家參考。
-
/*
-
* memset - Fill a region of memory with the given value
-
* @s: Pointer to the start of the area.
-
* @c: The byte to fill the area with
-
* @count: The size of the area.
-
*/
-
void *memset(void *s, int c, size_t
count)
-
{
-
char *xs = s;
-
-
while (count--)
-
*xs++ = c;
-
return s;
- }
- /*
- * memcpy - Copy one area of memory to another
- * @dest: Where to copy to
- * @src: Where to copy from
- * @count: The size of the area.
- */
- void *memcpy(void *dest, const void *src, size_t count)
- {
- char *tmp = dest;
- const char *s = src;
- while (count--)
- *tmp++ = *s++;
- return dest;
- }
-
/*
-
* memmove - Copy one area of memory to another
-
* @dest: Where to copy to
-
* @src: Where to copy from
-
* @count: The size of the area.
-
* Unlike memcpy(), memmove() copes
with overlapping areas.
-
*/
-
void *memmove(void *dest, const void *src, size_t
count)
-
{
-
char *tmp;
-
const char *s;
-
-
if (dest <= src) {
-
tmp = dest;
-
s = src;
-
while (count--)
-
*tmp++ = *s++;
-
} else {
-
tmp = dest;
-
tmp += count;
-
s = src;
-
s += count;
-
while (count--)
-
*--tmp = *--s;
-
}
-
return dest;
- }
-
/*
-
* memcmp - Compare two areas of memory
-
* @cs: One area of memory
-
* @ct: Another area of memory
-
* @count: The size of the area.
-
*/
-
int memcmp(const void *cs, const void *ct, size_t
count)
-
{
-
const unsigned char *su1, *su2;
-
int res = 0;
-
-
for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
-
if ((res = *su1 - *su2) != 0)
-
break;
-
return res;
- }
-
/*
-
* strcpy - Copy a %NUL terminated string
-
* @dest: Where to copy the string to
-
* @src: Where to copy the string from
-
*/
-
char *strcpy(char *dest, const char *src)
-
{
-
char *tmp = dest;
-
-
while ((*dest++ = *src++) != '\0');
-
-
return tmp;
- }
-
/*
-
* strncpy - Copy a length-limited, %NUL-terminated string
-
* @dest: Where to copy the string to
-
* @src: Where to copy the string from
-
* @count: The maximum number of bytes to copy
-
*
-
* The result is not %NUL-terminated if the
source exceeds
-
* @count bytes.
-
*
-
* In the case where the length of @src is less
than that of
-
* count, the remainder of @dest will be padded with %NUL.
-
*/
-
char *strncpy(char *dest, const char *src, size_t
count)
-
{
-
char *tmp = dest;
-
-
while (count) {
-
if ((*tmp = *src) != 0)
-
src++;
-
tmp++;
-
count--;
-
}
-
-
return dest;
- }
-
/*
-
* strcat - Append one %NUL-terminated string to another
-
* @dest: The string to be
appended to
-
* @src: The string to append to it
-
*/
-
char *strcat(char *dest, const char *src)
-
{
-
char *tmp = dest;
-
-
while (*dest)
-
dest++;
-
while ((*dest++ = *src++) != '\0');
-
-
return tmp;
- }
-
/*
-
* strncat - Append a length-limited, %NUL-terminated string to another
-
* @dest: The string to be
appended to
-
* @src: The string to append to it
-
* @count: The maximum numbers of bytes to copy
-
*
-
* Note that in contrast to strncpy(), strncat() ensures
the result is
-
* terminated.
-
*/
-
char *strncat(char *dest, const char *src, size_t
count)
-
{
-
char *tmp = dest;
-
-
if (count) {
-
while (*dest)
-
dest++;
-
while ((*dest++ = *src++) != 0) {
-
if (--count == 0) {
-
*dest = '\0';
-
break;
-
}
-
}
-
}
-
-
return tmp;
- }
-
/*
-
* strcmp - Compare two strings
-
* @cs: One string
-
* @ct: Another string
-
*/
-
int strcmp(const char *cs, const char *ct)
-
{
-
unsigned char c1, c2;
-
-
while (1) {
-
c1 = *cs++;
-
c2 = *ct++;
-
if (c1 != c2)
-
return c1 < c2 ? -1 : 1;
-
if (!c1)
-
break;
-
}
-
-
return 0;
- }
-
/*
-
* strncmp - Compare two length-limited strings
-
* @cs: One string
-
* @ct: Another string
-
* @count: The maximum number of bytes to compare
-
*/
-
int strncmp(const char *cs, const char *ct, size_t
count)
-
{
-
unsigned char c1, c2;
-
-
while (count) {
-
c1 = *cs++;
-
c2 = *ct++;
-
if (c1 != c2)
-
return c1 < c2 ? -1 : 1;
-
if (!c1)
-
break;
-
count--;
-
}
-
-
return 0;
- }
-
/*
-
* strchr - Find the first occurrence of a character in a string
-
* @s: The string to be
searched
-
* @c: The character to search for
-
*/
-
char *strchr(const char *s, int c)
-
{
-
for (; *s != (char)c; ++s)
-
if (*s == '\0')
-
return NULL;
-
-
return (char *)s;
- }
-
/*
-
* strlen - Find the length of a string
-
* @s: The string to be
sized
-
*/
-
size_t strlen(const char *s)
-
{
-
const char *sc;
-
-
for (sc = s; *sc != '\0'; ++sc);
-
-
return sc - s;
- }
-
/*
-
* strnlen - Find the length of a length-limited string
-
* @s: The string to be
sized
-
* @count: The maximum number of bytes to search
-
*/
-
size_t strnlen(const char *s, size_t
count)
-
{
-
const char *sc;
-
-
for (sc = s; count-- && *sc != '\0'; ++sc);
-
-
return sc - s;
- }
-
/*
-
* strsep - Split a string into
tokens
-
* @s: The string to be
searched
-
* @ct: The characters to search for
-
*
-
* strsep() updates @s to point
after the token, ready for the next call.
-
*/
-
char *strsep(char **s, const char *ct)
-
{
-
char *sbegin = *s;
-
char *end;
-
-
if (sbegin == NULL)
-
return NULL;
-
-
end = strpbrk(sbegin, ct);
-
if (end)
-
*end++ = '\0';
-
*s = end;
-
-
return sbegin;
- }
-
/*
-
* strstr - Find the first substring in a %NUL
terminated string
-
* @s1: The string to be
searched
-
* @s2: The string to search for
-
*/
-
char *strstr(const char *s1, const char *s2)
-
{
-
int l1, l2;
-
-
l2 = strlen(s2);
-
if (!l2)
-
return (char *)s1;
-
l1 = strlen(s1);
-
while (l1 >= l2) {
-
l1--;
-
if (!memcmp(s1, s2, l2))
-
return (char *)s1;
-
s1++;
-
}
-
-
return NULL;
- }
另外加上itoa(),atoi(),atof()實現(非內部實現)
1、itoa函數實現
- #include <stdio.h>
- void itoa(int i, char *string)
- {
- int power=0,j=0;
- j=i;
- for( power=1;j>10;j/=10)
- power*=10;
- for(;power>0;power/=10)
- {
- *string++='0'+i/power;
- i%=power;
- }
- *string='\0';
- printf("%s\n",string);
- }
- void main()
- {
- char string[20];
- itoa(12345, string);
- printf("%s\n",string);
- }
其中power相當於類似於1234,其power=1000;134,其power=100
*string++='0'+i/power;//獲得取得字符的asicii碼
i/power取得字符,例如1234/1000=1;234/100=2
2、atoi實現
- int atoi(char *str)
- {
- if(!str)
- return -1;
- bool bMinus=false;
- int result=0;
- if(('0'>*str || *str>'9')&&(*str=='+'||*str=='-'))
- {
- if(*str=='-')
- bMinus=true;
- *str++;
- }
- while( *str != '\0')
- {
- if('0'> *str || *str>'9')
- break;
- else
- result = result*10+(*str++ - '0');
- }
- if (*str != '\0')//no-normal end
- return -2;
- return bMinus?-result:result;
- }
重寫的atoi函數,沒有考慮溢出的情況。
if(('0'>*str || *str>'9')&&(*str=='+'||*str=='-'))//判讀第一個字符是否爲數字的正負號
if (*str != '\0')//no-normal end,當上文的while循環不正常退出,應視爲字符串不合法,例如“+1234abc”
測試:
- char *c1 = "12345";
- char *c2 = "-12345";
- char *c3 = "bat-123";
- char *c4 = "+123abc";
- printf("c1=%d\n",atoi(c1));
- printf("c2=%d\n",atoi(c2));
- printf("c3=%d\n",atoi(c3));
- printf("c4=%d\n",atoi(c4));
輸出結果爲:
c1=12345
c2=-12345
c3=-2
c4=-2
3.atof()
#include<stdio.h>
#include<string.h>
#define SIZE 50
double atof(char *s);
void main()
{
char s[SIZE];
printf("Input a string:\n");
gets(s);
atof(s);
}
double atof(char *s)
{
int i,j,k=0;
float total,head = 0.0,tail = 0.0;
for(i = 0;i<strlen(s);i++)
{
k++;
if(*(s+i)=='.')
break;
}
for(j=0;j<k-1;j++)
{
head = head*10 + *(s+j)-'0';
}
for(i=strlen(s)-1;i>=k;i--)
{
tail = tail/10.0 + (*(s+i)-'0')/10.0;
}
total = head + tail;
printf("S after atof:%f",total);
}