strcnpy函数
strncpy 是 C语言的库函数之一,来自 C语言标准库,定义于 string.h,char *strncpy(char *dest, const char *src, int n),把src所指向的字符串中以src地址开始的前n个字节复制到dest所指的数组中,并返回dest。
函数原型char *strncpy(char *dest,char *src,size_t n);
#include<stdio.h>
#include<assert.h>
char *My_strcnpy(char *des, char *src, int n)
{
assert(des != NULL && src != NULL);
while (*src != '\0' && n > 0)
{
*des++ = *src++;
n--;
}
*des = '\0';
return des;
}
void main()
{
char *str1 = "abcdef";
char str2[100] = {""};
My_strcnpy(str2, str1,4);
printf("%s\n", str2);
}
strncat函数
原型
char * strncat(char *dest, const char *src, size_t n);
【参数说明】:dest指向目标字符串,src为指向源字把src所指字符串的前n个字符添加到dest所指字符串的结尾处,并覆盖dest所指字符串结尾的’\0’,从而实现字符串的连接。
说明和dest所指内存区域不可以重叠,并且dest必须有足够的空间来容纳src的字符串。
返回值
返回指向dest的指针。
#include<stdio.h>
#include<assert.h>
char *My_strncat(char *des, char *str, int n)
{
assert(des != NULL && str != NULL);
while (*des != '\0')
{
des++;
}
while (*str != '\0' && n > 0)
{
*des++ = *str++;
n--;
}
*(++des) = '\0';
return des;
}
void main()
{
char str[100] = {"efgh"};
char des[100] = { "abcd" };
My_strncat(des,str,3);
printf("%s\n",des);
}
strcnmp函数
这个函数用来比较s1和s2字符串的前maxlen个字符。如果两个字符串相等的话,strncmp将返回0。如果s1是s2的一个子串的话,s1小于s2。此外还有,函数 int strncmp (const char *s1, const char *s2, size_t size) 此函数与strcmp极为类似。不同之处是,strncmp函数是指定比较size个字符。也就是说,如果字符串s1与s2的前size个字符相同,函数返回值为0。
原型:
int strncmp ( const char * str1, const char * str2, size_t num );
#include<stdio.h>
#include<assert.h>
int My_strcnmp(const char *str1, const char *str2, int n)
{
assert(str1 != NULL && str2 != NULL);
int len = 0;
while (*str1 != '\0' && *str2 != '\0' && n > 0)
{
if (*str1 != *str2)
{
len = *str1 - *str2;
return len;
}
str1++;
str2++;
}
n = *str1 - *str2;
return len;
}
void main()
{
char str1[100] = { "abcd" };
char str2[100] = {"abce"};
printf("%d\n",My_strcnmp(str1,str2,5));
}
strrchr函数
strrchr() 函数查找字符在指定字符串中从后面开始的第一次出现的位置,如果成功,则返回从该位置到字符串结尾的所有字符,如果失败,则返回 false。
#include<stdio.h>
#include<assert.h>
const char *my_strrchr(const char *str, int ch)//查找字符ch,在字符串出现的最后一次
{
assert(str != NULL);
int count = 0;
while (*str != '\0')
{
if (ch == *str)
{
count++;
}
if (count == 4)
{
return str;
}
str++;
}
return NULL;
}
void main()
{
char str[100] = {"abcdabcabaooo"};
int ch = 'a';
printf("%s\n",my_strrchr(str, ch));
}
strnchr函数
字符串查找给定字符,第n次出现的位置
#include<stdio.h>
#include<assert.h>
const char *my_strnchr(const char *str, char ch, int n)
{
assert(str != NULL);
while (*str != '\0')
{
if (ch == *str)
{
n--;
}
if (n == 0)
{
return str;
}
str++;
}
return NULL;
}
void main()
{
char str[100] = { "qwerqweqwqoo" };
char ch = 'q';
printf("%s\n", my_strnchr(str,ch,3));
}
Count_chars函数
查找第一个参数中,多少与第二个匹配,顺序无关
#include<stdio.h>
#include<assert.h>
int Count_chars(const char *str, const char *chars)//查找第一个参数中,多少与第二个匹配,顺序无关
{
assert(str != NULL && chars != NULL);
int count = 0;
while (*chars != '\0')
{
while (*str != '\0')
{
if (*str == *chars)
{
count++;
break;
}
str++;
}
chars++;
}
return count;
}
void main()
{
char str[100] = {"abc"};
char chars[100] = {"abcdef"};
int n = Count_chars(str,chars);
printf("%d\n",n);
}
未完待续……