#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "assert.h"
// 1 字符串的链接
char *strcat(char *strDes, const char *strSrc)
{
char *address = strDes;
assert((strDes != NULL)&&(strSrc != NULL));
while (*strDes != '\0')
{
++strDes;
}
while ((*strDes++ = *strSrc++) != '\0')
{
NULL;
}
return address;
}
void main01()
{
char str1[] = "12345";
char str2[] = "abcde";
printf("str:%s\n", strcat(str1, str2));
system("pause");
}
// 2 链接n个字符串
char *strncat1(char *strDes, const char *strSrc, int count)
{
char *address = strDes;
assert((strDes != NULL) && (strSrc != NULL));
while (*strDes != '\0')
{
++strDes;
}
while (count-- && *strSrc != '\0')
{
*strDes++ = *strSrc++;
}
*strDes = '\0';
return address;
}
void main02()
{
char str1[] = "12345";
char str2[] = "abcde";
printf("str:%s \n", strncat1(str1, str2, 2));
system("pause");
}
// 3 字符串比较
int strcmp(const char *s,const char *t)
{
assert((s != NULL) && (t != NULL));
while (*s && *t && *s == *t)
{
++s;
++t;
}
return (*s - *t);
}
void main03()
{
char str1[] = "12345";
char str2[] = "abcde";
int ret = strcmp(str1, str1);
printf("%d \n", ret);
system("pause");
}
// 4 字符串n比较
int strncmp(const char *s, const char *t, int count)
{
assert((s != NULL) && (t != NULL));
while (*s && *t && *s == *t && count--)
{
++s;
++t;
}
return (*s - *t);
}
void main04()
{
char str1[] = "12345";
char str2[] = "abcde";
int ret = strncmp(str1, str1, 3);
printf("结果ret = %d \n", ret);
system("pause");
}
// 5 字符串拷贝
char *strcpy1(char *strDes, const char *strSrc)
{
char *address = strDes;
assert((strDes != NULL) && (strSrc != NULL));
while ((*strDes++ = *strSrc++) != '\0')
{
NULL;
}
return address;
}
void main05()
{
char str1[] = "12345";
char str2[] = "abcde";
char *str = strcpy1(str1, str2);
printf("str:%s\n", str);
system("pause");
}
// 6 拷贝n个字符串
char *strncpy(char *strDes, char *strSrc, int count)
{
char *address = strDes;
assert((strDes != NULL) && (strSrc != NULL));
while(count-- && (*strDes++ = *strSrc++) != '\0')
{
NULL;
}
return address;
}
void main06()
{
char str1[] = "12345";
char str2[] = "abcde";
char *str = strncpy(str1, str2, 3);
printf("str:%s\n", str);
system("pause");
}
// 7 求取字符串的长度
int strlen1(const char *str)
{
int len = 0;
assert(str != NULL);
while (*str++ != '\0')
{
++len;
}
return len;
}
void main07()
{
char str1[] = "12345";
int ret = strlen1(str1);
printf("ret:%d\n", ret);
system("pause");
}
// 8 二分查找
int BSearch(elemtype *a, elemtype x, int low, int high)
{
// 在下届为low,上届为high的数组a中这般查找元素x
int mid;
if (low > high)
{
return -1;
}
mid = (low + high) / 2;
if (x = a[mid])
{
return mid;
}
if (x < a[mid])
{
return(BSearch(a,x, low, mid - 1));
}
else
{
return (BSearch(a, x, mid + 1, high));
}
return;
}
// 9 字符串逆序
char *Reserve(const char* str)
{
int i = 0;
char tmp;
char *address = str;
assert(str != NULL);
for (i = 0; i< strlen(str)/2; i++)
{
tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
return address;
}
void main()
{
char *str = "hello the world";
int len = strlen(str);
char *dest = (char *)malloc(len + 1);
char *address = dest;
char *end = &str[len - 1];
while (len-- != 0)
{
*address++ = *end++;
}
*address = 0;
system("pause");
}
c语言面试之字符串
最新推荐文章于 2023-07-06 18:13:03 发布