1.求非空字符串元素个数
#include <stdio.h>
int str_no_space(char* str)
{
int count = 0;
char* p = str;
while (*p)
{
if (*p != ' ')
{
count++;
}
p++;
}
return count;
}
int main(void)
{
char* str = "ni chou sha";
int ret = str_no_space(str);
printf("%d\n", ret);
return 0;
}
2.字符串逆置
#include <stdio.h>
void str_inverse(char* str)
{
char* start = str;
char* end = str + strlen(str) - 1;
while (start < end)
{
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main(void)
{
char str[] = "this is a test";
str_inverse(str);
printf("str = %s\n", str);
return 0;
}
3.判断字符串是回文
int abcba(char* str)
{
char* start = str;
char* end = str + strlen(str) - 1;
while (start < end)
{
if (*start != *end)
{
return 0;
}
start++;
end--;
}
return 1;
}
4.字符串处理函数
a.字符串拷贝 strcpy 和 strncpy
#include <stdio.h>
int main0301(void)
{
char src[] = "ni chou sha";
char dest[20] = { 0 };
char* p = strcpy(dest, src);
printf("p=%s\n", p);
printf("dest=%s\n", dest);
return 0;
}
int main(void)
{
char src[] = "hello world";
char dest[100] = { 0 };
char* p = strncpy(dest, src,100);
for (size_t i = 0; i < 10; i++)
{
printf("%c\n", p[i]);
}
printf("p=%s\n", p);
printf("dest=%s\n", dest);
return 0;
}
b.字符串拼接 strcat 和 strncat
#include <stdio.h>
int main(void)
{
char src[] = "world";
char dest[100] = "hello";
char* p = strncat(dest, src, 3);
printf("p=%s\n", p);
printf("dest=%s\n", dest);
return 0;
}
c.字符串比较 strcmp 和 strncmp
#include <stdio.h>
int main0501(void)
{
char* str1 = "helloworld";
char* str2 = "helloz";
printf("ret = %d\n", strcmp(str1, str2));
return 0;
}
int main(void)
{
char* str1 = "helloworld";
char* str2 = "helloz";
printf("ret = %d\n", strncmp(str1, str2, 8));
return 0;
}
d.字符串格式化输入,输出 scanf 和 sprintf
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
char buf[100] = { 0 };
sprintf(buf, "%d%c%d=%d\n", 10, '+', 34, 10 + 34);
puts(buf);
return 0;
}
int main(void)
{
int a, b, c;
char str[] = "13+56=69";
sscanf(str, "%d+%d=%d\n", &a, &b, &c);
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("c = %d\n", c);
return 0;
}
e.字符串查找字符,字串 strchr strrchr strstr
f.字符串分割 strtok
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "www.itcast.cn.com.net";
char* p = strtok(str, ".");
while (p != NULL)
{
p = strtok(NULL, ".");
printf("%s\n", p);
}
return 0;
}
int main(void)
{
char str[] = "www.itcast.cn$This is a strtok$test";
char* p = strtok(str, "$ .");
while (p != NULL)
{
p = strtok(NULL, " .$");
printf("%s\n", p);
}
return 0;
}
g.atoi/atof/atol
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = "10";
int num1 = atoi(str1);
printf("num1 = %d\n", num1);
char str2[] = "0.123";
double num2 = atof(str2);
printf("num2 = %lf\n", num2);
char str3[] = "123L";
long num3 = atol(str3);
printf("num3 = %ld\n", num3);
return 0;
}
5.局部变量,全局变量,static全局变量,static局部变量
6.全局函数,static函数
7.生命周期
8.内存4区模型
9.开辟释放 heap 空间
#include <stdio.h>
int main(void)
{
int* p = (int*)malloc(sizeof(int) * 1000000);
if (p == NULL)
{
printf("malloc error\n");
return -1;
}
for (size_t i = 0; i < 10; i++)
{
p[i] = i + 10;
}
for (size_t i = 0; i < 10; i++)
{
printf("%d ", p[i]);
}
free(p);
p = NULL;
return 0;
}
10.使用 heap 空间
11.二级指针对应的 heap 空间
#include <stdio.h>
int main(void)
{
int** p = malloc(sizeof(int*) * 3);
for (size_t i = 0; i < 3; i++)
{
p[i] = malloc(sizeof(int) * 5);
}
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 5; j++)
{
p[i][j] = i + j;
}
}
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 5; j++)
{
printf("%d ", p[i][j]);
}
printf("\n");
}
for (size_t i = 0; i < 3; i++)
{
free(p[i]);
p[i] = NULL;
}
free(p);
p = NULL;
return 0;
}
|