一.字符串检验
? 1.strlen:返回给定空终止字符串的长度
size_t my_strlen(const char* str);
2. strcmp:以字典序比较两个空终止字节字符串。结果的符号是被比较的字符串中首对不同字符的值间的差的符号。
int my_strcmp(const char* lhs, const char* rhs);
#include<stdio.h>
#include<assert.h>
#define N 30
int my_strcmp(const char* arr, const char* arr1)
{
assert(arr != NULL && arr1 != NULL);
while (*arr != '\0' && *arr1 != '\0')
{
if (*arr == *arr1)
{
arr++;
arr1++;
}
if (*arr < *arr1)
{
return -1;
}
else
{
return 1;
}
}
return 0;
}
int main()
{
char arr[N] = { "xuyeyyds" };
char arr1[N] = { "xuyeyyus" };
int l = my_strcmp(arr, arr1);
printf("%d\n", l);
return 0;
}
?3.strncmp:比较两个可能空终止字节字符串。结果的符号是被比较的字符串中首对不同字符(都转译成unsigned char)的值间的差的符号
4.strchr:查找字符的首次出现
#include<stdio.h>
#include<assert.h>
#define N 30
char* my_strchr(const char* arr, char ch)
{
if (arr == NULL)
{
return NULL;
}
char* ip = (char*)arr;
while (*ip != '\0' && *ip != ch)
{
ip++;
}
return ip;
}
int main()
{
char arr[N] = { "fkdgnfd" };
char arr1[N] = { "fkdgasd" };
char* p = NULL;
p=my_strchr(arr, 'g');
printf("%s\n",p);
return 0;
}
5.strrchr:查找字符的最后一次出现
#include<stdio.h>
#include<assert.h>
#define N 30
char* my_strrchr(const char* arr, char ch)
{
if (arr == NULL)
{
return NULL;
}
char* ip = (char*)arr;
while (*ip != '\0')
{
ip++;
}
while (*(ip-1) != '\0')
{
if (*ip == ch)
{
return ip;
}
ip--;
}
}
int main()
{
char arr[N] = { "fkdgngd" };
char* p = NULL;
p = my_strrchr(arr, 'g');
printf("%s\n", p);
return 0;
}
6.strstr:查找字串字符的首次出现
#include<stdio.h>
#include<assert.h>
#define N 30
int my_strlen(char* arr)
{
assert(arr != NULL);
int count = 0;
while (*arr != '\0')
{
arr++;
count++;
}
return count;
}
char* my_strrchr(const char* arr, const char* arr1)
{
if (NULL == arr || NULL == arr1)return NULL;
int n = my_strlen(arr);
int m = my_strlen(arr1);
for (int i = 0; i < n - m + 1; i++)
{
int k = i;
int j = 0;
while (j < m && arr[k] == arr1[j])
{
j++;
k++;
}
if (j == m)
{
return (char*)arr + i;
}
}
return NULL;
}
int main()
{
char arr[N] = { "fkdgwfkeongd" };
char arr1[N] = { "wfkeo" };
char* p = NULL;
p = my_strrchr(arr,arr1);
printf("%s\n", p);
return 0;
}
二.字符串操作
1.strcpy:复制一个字符串给另一个
#include<stdio.h>
#include<assert.h>
#define N 30
char* my_strcpy(char* dest, char* src)
{
assert(dest != NULL && src != NULL);
char* cp = dest;
while (*src != '\0' )
{
*cp = *src;
src++;
cp++;
}
*cp = '\0';
return dest;
}
int main()
{
char arr[N] = { "fkdgwfkeongd" };
char arr1[N] = { "wfkeo" };
my_strcpy(arr, arr1);
printf("%s\n", arr);
return 0;
}
?2.strncpy:从一个字符串复制一定数量的字符到另一个
#include<stdio.h>
#include<assert.h>
#define N 30
char* my_strncpy(char* dest, const char* src, size_t count)
{
assert(dest != NULL && src != NULL);
char* cp = dest;
while (count--)
{
*cp = *src;
cp++;
src++;
}
return dest;
}
int main()
{
char arr[N] = { "jkdgj" };
char arr1[N] = { "wfkeo" };
my_strncpy(arr, arr1,sizeof(arr1));
printf("%s\n", arr);
return 0;
}
?3.strcat:连接两个字符串
#include<stdio.h>
#include<assert.h>
#define N 30
char* my_strcat(char* dest, char* src)
{
assert(dest != NULL && src != NULL);
while (*dest != '\0')
{
dest++;
}
while (*src != '\0')
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return dest;
}
int main()
{
char arr[N] = { "jkdgj" };
char arr1[N] = { "wfkeo" };
my_strcat(arr, arr1);
printf("%s\n", arr);
return 0;
}
4.strncat:连接两个字符串的一定数量字符
#include<stdio.h>
#include<assert.h>
#define N 30
char* my_strncat(char* dest, const char* src,size_t count)
{
assert(dest != NULL && src != NULL);
char* cp = dest;
while (*cp != '\0')
{
cp++;
}
int ret = strlen(src);
while (ret--)
{
*cp = *src;
cp++;
src++;
if (strlen(dest) > count)
{
return NULL;
}
}
return dest;
}
int main()
{
char arr[N] = { "jkdgfghfyugoji" };
char arr1[N] = { "ppppp" };
char*p=my_strncat(arr, arr1, sizeof(arr)/sizeof(arr[0]));
printf("%s\n", p);
return 0;
}
三.内存操作函数&泛型函数
1.memset:以字符填充缓冲区
void *memset(void *dest,int ch,size_t count);
复制值ch(如同以(unsgined char)ch转换到unsgined char 后)到dest所指对象的首count个字节
#include<stdio.h>
#include<assert.h>
#define N 30
void* my_memset(void* dest, unsigned char val, size_t count)
{
if (NULL == dest)return dest;
char* cp = (char*)dest;
while (count--)
{
*cp = val;
cp = cp + 1;
}
return dest;
}
int main()
{
char arr[N] = {"dsjhfhujsdh"};
my_memset(arr, 'a', sizeof(arr));
for (int i = 0; i < N; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
?2.memcmp比较两块缓冲区
int memcmp(const void*dest,const void*src,size_t count);
比较dest,src所指对象的首个count字节。比较按字典序进行。
结果的符号是在被比较对象中相异的首个字节的值(都被转译成unsigned char)的差
若在dest和src所指向的任意对象结尾出现后访问,则行为未定义
#include<stdio.h>
#include<assert.h>
#define N 30
int my_memcmp(const char* dest, const char* src, size_t count)
{
assert(dest != NULL && src != NULL);
const char* cp = (const char *)dest;
const char* ip = (const char *)src;
while (count--)
{
if (*dest < *src)
{
return -1;
}
else if (*dest > *src)
{
return 1;
}
else
{
cp++;
ip++;
}
}
return 0;
}
int main()
{
char arr[N] = {"dsjhfhujsdh"};
char arr1[N] = { "sdfjhjwe" };
int x=my_memcmp(arr, arr1, sizeof(int)*6);
printf("%d\n", x);
return 0;
}
?
|