最近在准备 C 语言的上级考试,之前对 C 接触不多,在练习过程中把一些小知识点记录下来。
1. 字符串的截取
利用 strncpy 函数,传入三个参数,分别为目标字符串,起始位置,长度。 例如将日期字符串转化为数字,如 20120112
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main() {
char date1[20],date2[20]; scanf("%s",&date1); scanf("%s",&date2); char temp[4]; int year1 = atoi(strncpy(temp,date1,4)); int year2 = atoi(strncpy(temp,date2,4)); printf("year1:%d\n",year1); printf("year2:%d\n",year2); char temp2[2]; int month1 = atoi(strncpy(temp2,date1+4,2)); int month2 = atoi(strncpy(temp2,date2+4,2)); printf("month1:%d\n",month1); printf("month2:%d\n",month2); int day1 = atoi(strncpy(temp2,date1+6,2)); int day2 = atoi(strncpy(temp2,date2+6,2)); printf("day1:%d\n",day1); printf("day2:%d\n",day2); return 0;
}
|
以上便实现了输入一个日期然后对其进行分割的操作。
2. 二维数组的动态声明
利用 malloc 可以实现数组的动态声明
1 2 3 4 5 6 7 8 9
|
int **a; a = (int **)malloc(2*sizeof(int *)); int i,j; for (i = 0; i < 2; i ++) { a[i] = (int *)malloc(3*sizeof(int)); for (j = 0; j < 3; j++) { scanf("%d",&a[i][j]); } }
|
以上便实现了动态数组的分配,利用 scanf 为数组赋值
3. 二维数组的声明和初始化
头文件
初始化和测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int result[2][2]; for (i = 0; i < 2; i ++) { for (j = 0; j < 2; j++) { printf("%d ",result[i][j]); } printf("\n"); } memset(result,0,sizeof(int)*4); for (i = 0; i < 2; i ++) { for (j = 0; j < 2; j++) { printf("%d ",result[i][j]); } printf("\n"); }
|
结果
1 2 3 4
|
4196944 0 4195696 0 0 0 0 0
|
上述是数组的非动态声明
4. 快速排序
假设要排序的数组是 A [1]……A [N],首先任意选取一个数据(通常选用第一个数据)作为关键数据,然后将所有比它的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序。一趟快速排序的算法是: 1)设置两个变量 I、J,排序开始的时候 I=0,J=N-1; 2)以第一个数组元素作为关键数据,赋值给 X,即 X=A [0]; 3)从 J 开始向前搜索,即由后开始向前搜索,找到第一个小于 X 的值,两者交换; 4)从 I 开始向后搜索,即由前开始向后搜索,找到第一个大于 X 的值,两者交换; 5)重复第 3、4 步,直到 I=J; 例如:待排序的数组 A 的值分别是:(初始关键数据 X:=49) A [0] A [1] A [2] A [3] A [4] A [5] A [6] 49 38 65 97 76 13 27 进行第一次交换后: 27 38 65 97 76 13 49 ( 按照算法的第三步从后面开始找 ) 进行第二次交换后: 27 38 49 97 76 13 65 ( 按照算法的第四步从前面开始找 > X 的值,65>49, 两者交换,此时 I=3 ) 进行第三次交换后: 27 38 13 97 76 49 65 ( 按照算法的第五步将又一次执行算法的第三步从后开始找) 进行第四次交换后: 27 38 13 49 76 97 65 ( 按照算法的第四步从前面开始找大于 X 的值,97>49, 两者交换,此时 J=4 ) 此时再执行第三不的时候就发现 I=J,从而结束一躺快速排序,那么经过一躺快速排序之后: 27 38 13 49 76 97 65 即所有大于 49 的数全部在 49 的后面,所有小于 49 的数全部在 49 的前面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <stdio.h>
void quiksort(int a[],int low,int high) { int i = low; int j = high; int temp = a[i]; if( low < high) { while(i < j) { while((a[j] >= temp) && (i < j)) { j--; } a[i] = a[j]; while((a[i] <= temp) && (i < j)) { i++; } a[j]= a[i]; } a[i] = temp; quiksort(a,low,i-1); quiksort(a,j+1,high); } else { return; } } int main() { int arr[5] = {23,1,21,4,19}; quiksort(arr,0,4); int i; for(i=0;i<5;i++) { printf("%d ",arr[i]); } printf("\n"); return 0; }
|
快速排序代码如上
5. 字符串拷贝
小例子如下
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char str[20] = "hello"; char *a = "world"; strcpy(str,a); printf("%s",str); printf("\n"); system("pause"); return 0; }
|
运行结果:world,即把后者完全覆盖前者。
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char *str = new char[6]; char *a = "world"; strcpy(str,a); printf("%s", str); printf("\n"); system("pause"); return 0; }
|
运行结果一致 某一长度的字符串截取
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char *str = new char[6]; char *a = "world"; strncpy(str,a+1,5); printf("%s", str); printf("\n"); system("pause"); return 0; }
|
运行结果:orld
6. 字符串的拼接
小例子如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char str[20] = "hello "; char *a = "world"; char *x = strcat(str,a); printf("%s\n", x); printf("%s\n", x); printf("\n"); system("pause"); return 0; }
|
运行结果: hello world hello world 此函数既返回结果,又将目标字符串赋值
7. 字符串查找匹配
例子如下
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char str[20] = "hello "; char *x = strchr(str,'e'); printf("%d\n", x - str); printf("%s\n",x); printf("\n"); system("pause"); return 0; }
|
运行结果: 1 ello
8. 字符串比较
例子如下
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char str[20] = "hello "; char str2[20] = "hello2"; int x = strcmp(str, str2); printf("%d\n",x); printf("\n"); system("pause"); return 0; }
|
运行结果 - 1 忽略大小写
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main(){ char str[20] = "hello"; char str2[20] = "Hello"; int x = stricmp(str, str2); printf("%d\n",x); printf("\n"); system("pause"); return 0; }
|
运行结果 0
9. 字符串分割
示例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main() { char test1[] = "feng,ke,wei"; char x[3][30]; char *p; p = strtok(test1, ","); int count = 0; while (p) { printf("%s\n", p); strcpy(x[count],p); count++; p = strtok(NULL, ","); } for (int i = 0; i<count; i ++) { printf("%s ", x[i]); } system("pause"); return 0; }
|
运行结果 feng ke wei feng ke wei
10. 格式化输出几位小数
例如
则是输出 5 位小数 又如
1
|
printf("%5.1f",1.2345);
|
则是控制总位数为 5,小数点后为 1 位,不够的在前面补空格
文章来源: cuiqingcai.com,作者:崔庆才,版权归原作者所有,如需转载,请联系作者。
原文链接:cuiqingcai.com/1934.html
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)