C语言涉猎笔记(二)
字符串的一些操作
//字符串
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>//字符串操作需要导的包
int main() {
//第一种方式定义,char数组,打印遇'\0'才结束打印,'\0'不计入字符串长度
char str[] = {'K', 'e', 'v', 'i', 'n', '\0'};
str[2] = '2';//正常修改
printf("%s\n", str);
//第二种方式定义,指针,会默认结尾加上'\0'
char * str2 = "Kevin";
// str2[2] = '2'; // 会奔溃,报不允许访问的错
printf("%s\n", str2);
//获取字符串长度
int len = strlen(str);
printf("%d\n", len);
//字符串转换
char * num = "1";
//atoi是字符串转整形 atof是字符串转浮点类型
int result = atoi(num);
if (result) {
printf("转换成功:%d\n", result);
} else {
printf("转换失败\n");
}
//字符串比较(0代表相等)
char * a = "Kevin";
char * b = "kevin";
//区分大小写
int resultCompareA = strcmp(a, b);
//不区分大小写
int resultCompareB = strcmpi(a, b);
if (resultCompareA) {
printf("不相等\n");
} else {
printf("相等\n");
}
if (resultCompareB) {
printf("不相等\n");
} else {
printf("相等\n");
}
//字符串包含查找
char * text = "name is Kevin";
char * subtext = "is";
char * pop = strstr(text, subtext);
if (pop) {
printf("查到了, pop的值是:%s\n", pop);//pop == subtext以及后面的所有内容
} else {
printf("没查到, pop的值是:%s\n", pop);//pop == null
}
//通过指针减法获取第一次出现位置
int index = pop - text; // pop= "is" - "name is Kevin"
printf("%s第一次出现的位置是:%d\n", subtext, index);
//字符串截取
char * substr = "abcdefg";
char substr_new[10];
//第一个参数是截取后接收的容器,第二个参数是原字符串截取时指针位置,第三个参数是原字符串指针移动截取的长度
strncpy(substr_new, substr + 2, 2);
//目前知道只截取两位,所以在第三位设置'\0'方便打印
substr_new[2] = '\0';
printf("截取后字符串是:%s\n", substr_new);
//字符串拼接
char * string1 = "string1";
char * add = "+";
char * string2 = "string2";
//定义容器接收(类似于java的StringBuilder)
char new_str[20];
strcpy(new_str, string1); // 先Copy到数组里面去
strcat(new_str, add); // 然后再拼接
strcat(new_str, string2); // 然后再拼接
printf("拼接后的结果:%s\n", new_str); // string1+string2
//大小写互换
char * ds = "ABCdef";
char resultDs[10];
for (int i = 0; i <strlen(ds); ++i) {
if (islower(ds[i])) {
resultDs[i] = toupper(ds[i]);
} else if (isupper(ds[i])) {
resultDs[i] = tolower(ds[i]);
}
}
printf("%s\n", resultDs);//abcDEF
return 0;
}
结构体定义与使用
//结构体定义与使用
#include <stdio.h>
#include <string.h>
//struct 关键字定义结构体
struct Student {
//结构体成员属性
char name[10];
int age;
char sex;
char * qt;
//内部结构体,需要提前声明赋值
struct Other {
int other;
} other;
};
//结构体下面提前进行声明赋值,student1, student2, student3可以直接正常使用
struct Student2 {
//结构体成员属性
char name[10];
int age;
char sex;
char * qt;
//内部结构体
struct Other2 {
int other;
} other2;
} student1, student2, student3 = {"ls", 20, 'n', "qt3", {999}};
int main() {
//声明结构体(栈内存),成员属性为系统值
struct Student student;
//成员属性设置值(数组需要用拷贝)
strcpy(student.name, "kevin");
student.age = 18;
student.sex = 'n';
student.qt = "qt";
student.other.other = 888;
printf("name:%s, age:%d, sex:%c, qt:%s, other:%d\n", student.name, student.age, student.sex, student.qt, student.other.other);
//第二种方式,声明直接设置
struct Student studentX = {"ming", 25, 'v', "qtttt", {555}};
printf("name:%s, age:%d, sex:%c, qt:%s, other:%d\n", studentX.name, studentX.age, studentX.sex, studentX.qt, studentX.other.other);
//成员属性取值(数组需要用拷贝)
char name[10];
strcpy(name, student3.name);
int age = student3.age;
char sex = student3.sex;
char * qt = student3.qt;
int other = student3.other2.other;
printf("name:%s, age:%d, sex:%c, qt:%s, other:%d\n", name, age, sex, qt, other);
return 0;
}
结构体指针
//结构体指针
#include <stdio.h>
#include <string.h>
struct Student {
char name[10];
int age;
} student = {"kevin", 22};
int main() {
//结构体指针获取
struct Student * student_p = &student;
//结构体指针使用
student_p->age = 55;
strcpy(student_p->name, "kevin2");
printf("name:%s, age:%d \n", student_p->name, student_p->age);
return 0;
}
堆内存开辟结构体
//动态开辟结构体,堆内存开辟
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char name[10];
int age;
};
int main() {
//堆内存开辟结构体
struct Student * student = malloc(sizeof(struct Student));
strcpy(student->name, "li");
student->age = 28;
printf("name:%s, age:%d \n", student->name, student->age);
//堆内存的必须释放
free(student);
student = NULL;
return 0;
}
结构体数组
//结构体数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student{
char name[10];
int age;
};
int main() {
//栈内存开辟机构体数组
struct Student studentList[3] = {{"li", 18}, {"wan", 22}, {"zh", 25}};
//取出第二个元素的结构体,赋值给第三个元素的结构体
struct Student student2 = studentList[1];
*(studentList + 2) = student2;
for (int i = 0; i < 3; ++i) {
printf("name:%s, age:%d \n", studentList[i].name, studentList[i].age);
}
//堆内存开辟机构体数组
struct Student * StudentList_p = malloc(sizeof(struct Student) * 3);
for (int i = 0; i < 3; ++i) {
strcpy((StudentList_p + i)->name, "vi");
(StudentList_p + i)->age = 20 + i;
}
for (int i = 0; i < 3; ++i) {
printf("name:%s, age:%d \n", StudentList_p[i].name, StudentList_p[i].age);
}
free(StudentList_p);
StudentList_p = NULL;
return 0;
};
typedef关键字
//typedef 别名关键字 目的:解决平台差异性,方便书写代码
#include <stdio.h>
//typedef给结构体取了一个别名
typedef struct Student Stu;
struct Student{
char name[10];
int age;
};
//匿名结构体,必须声明别名进行使用,否则定义匿名没有意义
typedef struct {
char name[10];
int age;
} Tea;
int main(){
//直接使用别名创建结构体实例
Stu stu = {"kevin", 18};
printf("name:%s, age:%d \n", stu.name, stu.age);
Tea tea = {"green", 5};
printf("name:%s, age:%d \n", tea.name, tea.age);
return 0;
}
枚举
//枚举(和java一样)
#include <stdio.h>
enum CommentType {
TEXT = 10,
TEXT_IMAGE,
IMAGE
};
int main() {
enum CommentType commentType = TEXT;
enum CommentType commentType1 = TEXT_IMAGE;
enum CommentType commentType2 = IMAGE;
printf("%d, %d, %d \n", commentType, commentType1, commentType2);
return 0;
}