day12: 字符串处理函数 sprintf, sscanf; 只读类型 const
This commit is contained in:
		
							parent
							
								
									4e670ac3a6
								
							
						
					
					
						commit
						b75f87d375
					
				
							
								
								
									
										15
									
								
								day12/d3.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								day12/d3.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| // 格式化输出字符串 sprintf(char *buf, const char *format, ...)
 | ||||
| // 函数返回输出到 buf 字符串,并返回 buf 的字符串长度
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char buf[20] = ""; | ||||
|     // ret 返回字符串 长度
 | ||||
|     // %0nd :  n为输出长度,不足n位的前面位置补0
 | ||||
|     int ret = sprintf(buf, "%d 年 %02d 月 %d 日", 2023, 7, 18); | ||||
|     printf("ret = %d,%s\n", ret, buf); | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										12
									
								
								day12/d3_2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								day12/d3_2.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| // sprintf() 实现字符串与字符串拼接,字符串与数字拼接,数字与数字拼接
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char buf1[20] = "hello"; | ||||
|     char buf2[20] = "world"; | ||||
|     sprintf(buf1, "%s %s", buf1, buf2); | ||||
|     printf("%s", buf1); | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										18
									
								
								day12/d4.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								day12/d4.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| // 从字符串获取 sscanf(const char *buf, const char *format, ...);
 | ||||
| // 从 buf 字符串按 format 格式提取数据并赋值给后面的变量地址
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char *content = "disen,17791692095,610039018"; | ||||
|     char name[10] = ""; // 换行符占最后一位
 | ||||
|     long phone; | ||||
|     char qq[10] = ""; | ||||
| 
 | ||||
|     sscanf(content, "%5s,%ld,%9s", name, &phone, qq); | ||||
|     // sscanf(content, "%5s,%ld,%9s", name, &phone, qq);
 | ||||
|     printf("name: %s\nphone: %ld\nqq: %s\n", name, phone, qq); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										18
									
								
								day12/d5.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								day12/d5.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| // sscanf() 高级用法: 跳过内容
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char *content = "disen,17791692095,610039018"; | ||||
|     char first = 0; | ||||
|     char phone[12] = ""; | ||||
|     char qq[12] = ""; | ||||
|     sscanf(content, "%c", &first); | ||||
|     printf("%c\n", first); | ||||
|     // 跳过前6个字符,然后读取11个字符,放入 phone 中
 | ||||
|     sscanf(content, "%*6s%11s", phone); | ||||
|     printf("%s\n", phone); | ||||
|     sscanf(content, "%*18s%11s", qq); | ||||
|     printf("%s\n", qq); | ||||
| } | ||||
							
								
								
									
										16
									
								
								day12/d5_2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								day12/d5_2.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| // sscanf() 高级用法: 跳过空格
 | ||||
| // 遇到空格则跳过
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char *content = "disen 17791692095,610039018"; | ||||
|     char phone[12] = ""; | ||||
|     // *s 表示跳过空格, 11s 表示读取11个字符
 | ||||
|     sscanf(content, "%*s%11s", phone); | ||||
|     // sscanf(content, "%*[a-z]%11s", phone);
 | ||||
|     printf("%s\n", phone); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										18
									
								
								day12/d5_3.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								day12/d5_3.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| // 提取内容 bc#def@ghi 中的 # 两边的内容
 | ||||
| // 都是贪婪匹配, 会一直读取到 # 为止
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char *content = "bc#def@ghi"; | ||||
|     char buf1[20] = ""; | ||||
|     char buf2[20] = ""; | ||||
|     // %[^#] 表示读取到 # 为止的内容, %[^@] 表示读取到 @ 为止的内容
 | ||||
|     sscanf(content, "%[a-z]#%[a-z@]", buf1, buf2); | ||||
| 
 | ||||
|     printf("%s\n", buf1); | ||||
|     printf("%s\n", buf2); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										18
									
								
								day12/d5_4_test.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								day12/d5_4_test.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char *content = "disen:17791692095\njack:18799101234\nlucy:19088675432"; | ||||
|     char phone[3][12]; | ||||
|     // * 用来表示跳过内容
 | ||||
|     sscanf(content, "%*[^:]:%s%*[^:]:%s%*[^:]:%s", phone[0], phone[1], phone[2]); | ||||
|     // sscanf(content, "%*[^0-9]%[0-9]%*[^0-9]%[0-9]%*[^0-9]%[0-9]", phone[0], phone[1], phone[2]);
 | ||||
|     // sscanf(content, "%*[^0-9]%s%*[^0-9]%s%*[^0-9]%s", phone[0], phone[1], phone[2]);
 | ||||
|     // sscanf(content, "%*[a-z:\n]%s%*[a-z:\n]%s%*[a-z:\n]%s", phone[0], phone[1], phone[2]);
 | ||||
| 
 | ||||
|     for (int i = 0; i < 3; i++) | ||||
|         printf("%s\n", phone[i]); // 循环打印
 | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										14
									
								
								day12/d6.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								day12/d6.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| // const 修饰变量,表示只读变量
 | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     const int m = 10; | ||||
|     // m = 20; // 编译时报错: 因为 m 是只读变量
 | ||||
| 
 | ||||
|     int *p = &m; // 地址可以赋值给指针变量
 | ||||
|     *p = 20;     // 可以通过指针变量修改只读变量的值
 | ||||
| 
 | ||||
|     printf("%d\n", m); | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										14
									
								
								day12/d6_2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								day12/d6_2.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     const int m = 10; | ||||
| 
 | ||||
|     const int *p = &m; // 地址可以赋值给指针变量
 | ||||
| 
 | ||||
|     int n = 50; | ||||
|     p = &n; // 可以修改指针变量的指向,指针从指向 m 变成指向 n
 | ||||
| 
 | ||||
|     printf("%d\n", *p); | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										14
									
								
								day12/d7.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								day12/d7.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| // const 修饰指针变量 p, 使得 p 变成只读变量, 不能指向新的地址
 | ||||
| // 指针变量是只读 (不能指向新的地址)
 | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     const int n = 10; | ||||
|     int *const p = &n; // 必须初始化, 不等于 const int *p = &n;
 | ||||
|     *p = 20;           // 可以修改指针变量指向的内容
 | ||||
|     printf("%d\n", *p); | ||||
|     int m = 100; | ||||
|     // p = &m; // 编译时报错: 因为 p 是只读变量
 | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										16
									
								
								day12/d8.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								day12/d8.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| // const 修饰 * 和指针变量
 | ||||
| 
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     int m = 100; | ||||
|     const int *const p = &m; // 第一个 const 修饰 *p, 第二个 const 修饰 p
 | ||||
|     *p += 10;                // 编译时报错: 因为 *p 是只读变量
 | ||||
|     int n = 90; | ||||
|     p = &n; // 编译时报错: 因为 p 是只读变量
 | ||||
|     printf("%d\n", *p); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										13
									
								
								day12/d8_test1.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								day12/d8_test1.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,13 @@ | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|     char m[30] = "lucy, 666"; | ||||
|     char *const p = m; // 必须初始化, 不等于 const char *p = m;
 | ||||
|     p[0] = 'd'; | ||||
|     *(p + 1) = 'o'; | ||||
|     printf("%s\n", p); | ||||
|     // p = "jack"; // 编译时报错: 因为 p 是只读变量
 | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user