This commit is contained in:
flykhan 2023-07-10 17:46:22 +08:00
parent 26bd3514b6
commit 895035f176
9 changed files with 146 additions and 0 deletions

BIN
day6/a.out Executable file

Binary file not shown.

19
day6/d12.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
int main()
{
char teacher[] = {'d', 'i', 's', 'e', 'n'};
printf("%s\n", teacher); // disen
int len = sizeof(teacher);
printf("%d\n", len); // 5
// for (int i = 0; i < len; i++)
// {
// printf("%c", teacher[i]);
// }
int i = 0;
while (i < len)
{
printf("%c", teacher[i++]);
}
return 0;
}

18
day6/d13.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>
int main()
{
char ask_name[32] = "Jack"; // 结果是 "Jack\0"
printf("%s\n", ask_name);
// sizeof() 统计变量或数组实际分配的内存空间大小,单位是字节
printf("ask_name size is %ldB\n", sizeof(ask_name));
// strlen() 函数统计有效的字符个数,不包括 '\0' (遇到第一个 '\0' 就停止统计 )
printf("ask_name char len is %ld\n", strlen(ask_name));
char q_what1[128] = "hi,xiao hua\0,love you!";
printf("%s\n", q_what1);
printf("%ld \n", strlen(q_what1));
return 0;
}

23
day6/d14.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
int main()
{
char arr[64] = "hi, disen!";
// 实现 strlen() 的功能, 统计字符串的长度
int i = 0;
while (arr[i])
i++;
printf("%s length: %d\n", arr, i);
// 反转字符串
int j = 0;
int mid = i >> 1; // 含义: i / 2, 位运算更快
while (j < mid)
{
arr[j] ^= arr[i - j - 1];
arr[i - j - 1] ^= arr[j];
arr[j] ^= arr[i - j - 1];
j++;
}
printf("%s\n", arr);
}

20
day6/d15.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <string.h>
int main()
{
char cmd[32] = "";
printf("请输入一个命令:");
// scanf() 输入字符串是,遇到第一个空格就停止输入
// scanf("%s", cmd); // cmd 数组传递的是地址, 不需要加 & 取地址符
fgets(cmd, sizeof(cmd), stdin); // 读取一行字符串,遇到回车就停止输入
// fgets() 获取的字符最后一个是 '\n' 换行符
cmd[strlen(cmd) - 1] = 0; // 将最后一个字符 '\n' 换成 '\0' 空字符
// printf("%s \n", cmd);
// for (int i = 0; i < 32; i++)
printf("%s, %ld\n", cmd, strlen(cmd));
printf("\n");
return 0;
}

25
day6/d17.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <string.h>
int main()
{
// char stu_names[5][32] = {"zhangsan", "lisi", "wangwu", "zhaoliu", "tianqi"};
char stu_names[5][32];
int i = 0;
while (i < 5)
{
printf("请输入第 %d 个学生的名字:", i + 1);
fgets(stu_names[i], sizeof(stu_names[i]), stdin);
stu_names[i][strlen(stu_names[i]) - 1] = '\0';
i++;
}
for (int i = 0; i < 5; i++)
{
printf("%s\n", stu_names[i]);
}
return 0;
}

2
day6/d18.c Normal file
View File

@ -0,0 +1,2 @@
#include <stdio.h>
#include <string.h>

6
day6/d19.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
#include <string.h>
int main()
{
}

33
day6/d6.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
int main()
{
int nums[5][3] = {0};
int n;
for (int i = 0; i < 5; i++)
{
printf("请输入第%d个三位数", i + 1);
scanf("%d", &n);
int m[] = {100, 10, 1};
int j = 0;
while (n > 0)
{
nums[i][j] = n / m[j];
n %= m[j];
j++;
}
}
for (int i = 0; i < 5; i++)
{
// 判断这个三位数是否为回文数
if (nums[i][0] == nums[i][2])
continue;
for (int j = 0; j < 3; j++)
{
printf("%d ", nums[i][j]);
}
printf("\n");
}
}