最近因为工作需要开始重新学c语言,越学越发现c语言深不可测,当初用python轻轻松松处理的一些数据,但是c语言写起来却异常的复杂,这个板块就记录一下我的c语言复习之路
1、原型:size_t strlen(char const* string);
2、功能:返回字符串 string 的长度(不包含字符串终止符NUL)
3、注意:size_t是一个无符号整数类型
举例:
#include
#include
#include
int main()
{
char* y = "abcdef";
char* x = "abcd";
if (strlen(x) >= strlen(y)) {
printf("1: strlen(x) >= strlen(y)\n");
} else {
printf("1: strlen(x) < strlen(y)\n");
}
/* 由于strlen(x)返回的是一个size_t,所以strlen(x) - strlen(y) >= 0恒成立,
* 导致出现错误的结论
*/
if (strlen(x) - strlen(y) >= 0) {
printf("2: strlen(x) - strlen(y) >= 0\n");
} else {
printf("2: strlen(x) - strlen(y) < 0\n");
}
// 将size_t转换为int类型后,可以返回正确的值
if ((int)(strlen(x)) - (int)(strlen(y)) >= 0) {
printf("3: (int)strlen(x) - strlen(y) >= 0\n");
} else {
printf("3: (int)strlen(x) - strlen(y) < 0\n");
}
return 0;
}
值得注意的是size_t是一个无符号整数类型,因此
strlen(x) - strlen(y) >= 0恒成立
因此用strlen做长度对比的时候直接使用strlen(x) >= strlen(y)
这个式子就可以了
手动实现
unsigned int my_strlen(char* str)
{
int count = 0;
while (*str++ != '\0')
{
count++;
}
return count;
}
char *strcpy(char *dst, char const *src);
#include
#include
#include
int main()
{
char msg[] = "Original message";
printf("before strcpy: msg:%s\n", msg);
strcpy(msg, "Different");
printf("after strcpy: msg:%s\n", msg);
return 0;
}
手动实现
char* my_strcpy(char* str1, char* str2)
{
char* target1 = str1;
char* target2 = str2;
while((*target1++ = *target2++));
return str1;
}
char *strncpy(char *dst, char const *src, size_t len);
#include
#include
#include
#define STR_CPY_LEN 5
int main()
{
char msg[] = "Original message";
printf("before strncpy: msg:%s\n", msg);
strncpy(msg, "Different", STR_CPY_LEN);
printf("after strncpy: msg:%s\n",msg);
return 0;
}
手动实现
char* my_strncpy(char* str1, const char* str2, int n)
{
char* target1 = str1;
char* target2 = str2;
while(n--)
{
if (*target1 == '\0') break;
*target1++ = *target2++;
}
*target1 = '\0';
return str1;
}
char *strcat(char *dst, char const *src);
strcat 函数原理:dst 内存空间大小 = 目标字符串长度 + 原始字符串场地 + ‘\0’
,可以用char a[100]; char* a = (char*)malloc(100)这两种方式先分配出内存空间
#include
#include
#include
int main()
{
char msg[20] = "hello";
printf("before strcat: msg:%s\n", msg);
strcat(msg, ", world.");
printf("after strcat: msg:%s\n", msg);
return 0;
}
手动实现
char* my_strcat(char* str1, const char* str2)
{
char *ret = str1;
while(*++str1 != '\0');
while((*str1++ = *str2++));
return ret;
}
这里测试的时候
str1
的存储空间一定要是str2
存储空间的两倍以上
int strcmp(char const *s1, char const *s2);
#include
#include
#include
int main()
{
char *s1 = "hello";
char *s2 = "hello, world";
int ans = strcmp(s1, s2);
if (ans > 0) {
printf("s1 > s2\n");
} else if (ans < 0) {
printf("s1 < s2\n");
} else {
printf("s1 = s2\n");
}
return 0;
}
char *itoa(int value, char *str, int base);
(sizeof(int)*8+1)
个字节来存放。比如在 16 位平台上,需要 17 个字节来存放;在 32 位平台上,则需要 33 个字节来存放。例子:
#include
#include
int main ()
{
int i;
char buffer[33];
printf("Enter a number: ");
scanf("%d", &i);
itoa(i, buffer, 10);
printf("decimal: %s\n", buffer);
itoa(i, buffer,16);
printf("hexadecimal: %s\n", buffer);
itoa(i, buffer, 2);
printf("binary: %s\n", buffer);
return 0;
}
自我实现
char *myitoa(int num, char *str)
{
int dec = 1;
int i = 0;
int temp;
if (num < 0)
{
str[i++] = '-';
num = -num;
}
temp = num;
while (temp > 9)
{
dec *= 10;
temp /= 10;
}
while (dec != 0)
{
str[i++] = num / dec + '0';
num = num % dec;
dec /= 10;
}
str[i] = '\0';
return str;
}
恕我直言,python直接一个int函数梭哈,c语言真是花里胡哨
int sprintf(char *str, const char *format, ...)
举例:
#include
#include
int main()
{
char* strs = (char*)malloc(30);
sprintf(strs, "python %s java %d", "cpp", 666);
puts(strs);
free(strs);
return 0;
}
其实说白了就是一个字符拼接函数类似于python的
"xxx{}".format(xx)
和f"xxx{xx}"
#include
#include
int main()
{
char* strs = (char*)malloc(30);
sprintf(strs, "python %s java %s", "cpp", "rust");
puts(strs);
free(strs);
return 0;
}
sprintf还有其他比较细致的用法,详情可以参考https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm