长度不受限制的字符串函数:
模拟实现strcat(字符串拼接):
格式:
char * strcat(char * destination, const char * source);
#include<stdio.h>
#include<windows.h>
#include<assert.h>
char*my_strcat(char*dst, const char*src)
{
assert(dst);
assert(src);
char*ret = dst;
while (*dst)
{
dst++;
}
while (*dst = *src)
{
dst++, src++;
}
return ret;
}
int main()
{
const char str[32] = "abcdef";
char buf[32] = "123456";
my_strcat(str, buf);
printf("%s\n", str);
system("pause");
return 0;
}
模拟实现strcpy(字符串拷贝):
格式:
char *strcpy(char * destination, const char * source);
#include<stdio.h>
#include<windows.h>
#include<assert.h>
char*my_strcpy(char*dst, const char*src)
{
assert(dst);
assert(src);
char*ret = dst;
while (*dst=*src)
{
dst++;
src++;
}
return dst;
}
int main()
{
const char*str = "abcdefg";
char buf[32] ;
my_strcpy(buf,str);
printf("%s\n", buf);
system("pause");
return 0;
}
模拟实现strcmp(字符串比较):
格式:
int strcmp(const char * str1, const char * str2);
#include<stdio.h>
#include<windows.h>
int my_strcmp(const char*str1, const char*str2)
{
int res = 0;
int ret = 0;
while (!(ret = *(unsigned char*)str1 - *(unsigned char*)str2) && *str1)
{
str1++;
str2++;
}
if (ret > 0)
{
res = 1;
}
else if (ret < 0)
{
res = -1;
}
else
{
res = 0;
}
return res;
}
int main()
{
const char*str1 = "abcdefg";
const char*str2 = "abc";
printf("%d\n", my_strcmp(str1, str2));
system("pause");
return 0;
}
求字符串长度:
实现strlen的三种方法:
格式:
size_t strlen(const char * str);
1).计数器方式:
int my_strlen(const char*str)
{
int count = 0;
while (*str)
{
count++;
str++;
}
return count;
}
2).不能创建临时变量:
int my_strlen(const char*str)
{
if (*str == '\0')
{
return 0;
else
{
return 1 + my_strlen(str + 1);
}
}
3).指针—指针的方式:
int my_strlen( char*str)
{
char *p = str;
while (*p != '\0')
{
p++;
}
return p - str;
}
长度受限制的字符串函数:
模拟实现strncpy:
格式:
char * strncpy(char * destination, const char * source, size_t num);
#include<stdio.h>
#include<windows.h>
#pragma warning (disable:4996)
int main()
{
char str[32] = "abcd";
char str1[32] = "xyz";
strncpy(str, str1, strlen(str1) + 1);
printf("%s\n", str);
system("pause");
return 0;
}
注意:strncpy在拷贝的时候,要以“\0"结尾,所以“strlen(str1)+1”中要加上“1”
模拟实现strncat:
格式 :
char * strncat( char * destination, const char * source, size_t num);
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str1[20];
char str2[20];
strcpy(str1, "To be");
strcpy(str2, "or not to be");
strncat(str1, str2, 6);
puts(str1);
system("pause");
return 0;
}
注意:
strncat默认自动添加“\0"
模拟实现strncmp:
格式:
int strncmp ( const char * str1, const char * str2, size_t num );
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[][5] = { "R2D2", "C3P0", "R2A6" };
int n;
puts("Looking for R2 astromech droids...");
for (n = 0; n < 3; n++)
if (strncmp(str[n], "R2xx",2)==0)
{
printf("found %s\n", str[n]);
}
system("pause");
return 0;
}
字符串查找:
模拟实现strchr:
格式:
char * strchr ( const char *, int);
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[] = "This is a sample string";
char*pch;
printf("Looking for the 's' character in \"%s\"...\n", str);
pch = strchr(str, 's');
while (pch != NULL)
{
printf("found at %d\n", pch - str + 1);
pch = strchr(pch + 1, 's');
}
system("pause");
return 0;
}
模拟实现strrchr:
格式:
char * strrchr ( const char *, int);
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[] = "This is a sample string";
char*pch;
pch = strrchr(str, 's');
printf("Last occurence of 's' found at %d \n", pch - str + 1);
system("pause");
return 0;
}
模拟实现strpbrk:
格式:
char * strpbrk(const char *, const char * );
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[] = "This is a sample string";
char key[] = "aeiou";
char*pch;
printf("Vowels in '%s':", str);
pch = strpbrk(str, key);
while (pch != NULL)
{
printf("%c", *pch);
pch = strpbrk(pch + 1, key);
}
printf("\n");
system("pause");
return 0;
}
模拟实现strstr:
格式:
char * strstr ( const char *, const char * );
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[] = "This is a sample string";
char*pch;
pch = strstr(str,"simple");
strncpy(pch, "sample", 6);
puts(str);
system("pause");
return 0;
}
错误信息报告:
模拟实现strerror:
功能:返回错误码所对应的错误信息(即将错误码转成相应的描述)
格式:
char * strerror ( int errnum )
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
FILE*pFile;
pFile = fopen("unexist.ent","r");
if (pFile == NULL)
{
printf("Error opening file unexist.ent:%s\n", strerror);
}
system("pause");
return 0;
}
高级字符串查找:
模拟实现strtok:
功能:分隔有效字符串,且一次只能将一个字符串,一分为二。(对原先已有的分隔符不作处理)
格式:
char * strtok(char * str, const char * sep);//char *str是目标字符串;const char * sep是分隔符集合
1)sep参数是个字符串,定义了用作分隔符的字符集合。
2)第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。
3)strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。
4)strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
5)strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
6)如果字符串中不存在更多的标记,则返回NULL。
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[] = "- This, a sample string.";
char*pch;
printf("Splitting string \"%s\" into tokens:\n", str);
pch = strtok(str, " ,.-");
while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, " ,.-");
}
system("pause");
return 0;
}
模拟实现strspn:
格式:
size_t strspn(const char * str1, const char * str2 );
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
int i;
char strtext[] = "129th";
char cset[] = "1234567890";
i = strspn(strtext, cset);
printf("The initial number has %d digits.\n", i);
system("pause");
return 0;
}
模拟实现strcspn:
格式:
size_t strcspn ( const char * str1, const char * str2 );
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main()
{
char str[] = "fcba73";
char keys[] = "1234567890";
int i;
i = strcspn(str, keys);
printf("The first number in str is at position %d.\n", i + 1);
system("pause");
return 0;
}
字符分类函数:
下面是实现字符转换的代码:
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<ctype.h>
int main()
{
int i = 0;
char str[] = "Test String.\n";
char c;
while (str[i])
{
c = str[i];
if (isupper(c))
{
c = tolower(c);
putchar(c);
i++;
}
}
system("pause");
return 0;
}
内存操作函数:
模拟实现memcpy(内存字符串拷贝)
格式:
void * memcpy ( void * destination, const void * source,size_t num );
函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
这个函数在遇到“\0”的时候并不会停下来。
如果source和destination有任何的重叠,复制的结果都是未定义的。
1).拷贝整数:
#include<stdio.h>
#include<windows.h>
#include<assert.h>
void *my_memcpy(void*dst, const void*src, int num)
{
assert(dst);
assert(src);
char*_dst = (char*)dst;
const char*_src = (const char*)src;
while (num--)
{
*_dst = *_src;
_dst++;
_src++;
}
return dst;
}
int main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int b[10];
my_memcpy(b, a, sizeof(a));
system("pause");
return 0;
}
2).memcpy 拷贝字符串:
#include<stdio.h>
#include<windows.h>
#include<assert.h>
void *my_memcpy(void*dst, const void*src, int num)
{
assert(dst);
assert(src);
char*_dst = (char*)dst;
const char*_src = (const char*)src;
while (num--)
{
*_dst = *_src;
_dst++;
_src++;
}
return dst;
}
int main()
{
char str[10] = "abcdef";
char str1[10];
my_memcpy(str1, str, strlen(str) + 1);
system("pause");
return 0;
}
模拟实现memmove:
格式:
void * memmove ( void * destination, const void * source,size_t num );
#include<stdio.h>
#include<windows.h>
#include<assert.h>
void *my_memmove(void*dst, const void*src, int num)
{
assert(dst);
assert(src);
void*ret = dst;
if (dst <= src || (char*)dst >= ((char*)src+num))
{
while (num--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else
{
dst = (char*)dst + num - 1;
src = (char*)src + num - 1;
while (num--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return (ret);
}
int main()
{
char str[10] = "abcdefg";
my_memmove(str, str + 1, sizeof(str)+1);
system("pause");
return 0;
}
memmove存重叠的情况:
模拟实现memcmp
格式:
int memcmp(const void*ptr1, const void*ptr2, size_t num);
#include<stdio.h>
#include<windows.h>
int main()
{
char buffer1[] = "DWga0tP12df0";
char buffer2[] = "DWGA0TP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0)
{
printf("'%s' is greater than '%s'.\n", buffer1,buffer2);
}
else if (n < 0)
{
printf("'%s' is less than '%s'.\n", buffer1, buffer2);
}
else
printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
system("pause");
return 0;
}
模拟实现memchr
格式:
void *memchr(const void*, int, size_t);
#include<stdio.h>
#include<string.h>
#include<windows.h>
int main()
{
char*pch;
char str[] = "Example string";
pch = (char*)memchr(str, 'p', strlen(str));
if (pch != NULL)
{
printf("'p' found at position %d.\n", pch - str + 1);
}
else
printf("'p' not found.\n");
system("pause");
return 0;
}