C语言面试题(二)
上篇对嵌入式中C语言基本数据类型,关键字和常用操作进行了汇总,这篇我们将侧重字符串操作。请看下面的字符串处理函数:
a.库函数
1)将字符串src拷贝到字符数组dest内
char *strcpy(char *dest, const char *src);
dest 要输入的字符数组名,内存的地址
src 要复制的字符数组名,内存地址
1 /************************************************************************* 2 > File Name: strcpy_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 04:29:07 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void) 12 { 13 char str[]="asdfjkladfeifdfjkjfljfljlasjf"; 14 char dest[BUFSIZ]=""; 15 char ch[] = "ab"; 16 strcpy(dest,str); 17 printf("dest = %s\n",dest); 18 printf("sizeof(ch) = %ld\n",sizeof(ch));//include \0 19 return 0; 20 }
2)求字符串长度
size_t strlen(const char *s);
字符串长度:是'\0'前面的字母个数,不包含'\0'
s 要计算长度的字符串,或存字符串内存的地址
1 /************************************************************************* 2 > File Name: strlen_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 06:35:25 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void) 12 { 13 char str[] = "qwertyuasdfgj"; 14 int len; 15 len = strlen(str); 16 printf("strlen(str) = %d,%s\n",len,str);//not inlcude \0 17 return 0; 18 }
3)比较字符串的大小
int strcmp(const char *s1, const char *s2);
s1,s2,是两个要比较的字符串
返回的值 <0 代表s1小,=0 相等 >0 s1大
1 /************************************************************************* 2 > File Name: strcmp_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 06:58:19 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void) 12 { 13 char *str = "adfndfkkf"; 14 char *dest = "acdffgf"; 15 16 if(strcmp(dest,str) == 0) 17 { 18 printf("str == dest\n"); 19 } 20 else if(strcmp(dest,str) < 0) 21 { 22 printf("dest < str\n"); 23 } 24 else 25 { 26 printf("dest > str\n"); 27 } 28 29 return 0; 30 }
4)字符串拼接
char *strcat(char *dest, const char *src);
dest 目标的地址,目标的字符数组
src 源地址,是将src字符串连接在dest字符串后面
1 /************************************************************************* 2 > File Name: strcat_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 07:14:01 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void) 12 { 13 char *str = "abcdefg"; 14 char dest[BUFSIZ] = "higklmn"; 15 16 17 printf("strlen(str) == %d\n",strlen(str)); 18 printf("strlen(dest) == %d\n",strlen(dest)); 19 strcat(dest,str); 20 printf("String cating. . .\n"); 21 sleep(3); 22 printf("dest == %s\n",dest); 23 printf("strlen(dest) == %d\n",strlen(dest)); 24 printf("sizeof(dest) == %d\n",sizeof(dest)); 25 return 0; 26 }
5)在字符串中查找另一个字符串
char *strstr(const char *haystack, const char *needle);
haystack 一个字符串
needle 要寻找字符串,在haystack里面查找needle这个字符串
返回值,是在haystack内找到的字符串的地址或者说,是找到的字符数组
1 /************************************************************************* 2 > File Name: strstr_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 07:32:36 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void) 12 { 13 char *haystack = "abcdadecdakllgd"; 14 char *needle = "b"; 15 char *temp; 16 17 temp = strstr(haystack,needle); 18 printf("temp == %s\n",temp); 19 return 0; 20 }
6)从键盘上获取字符串
char *gets(char *s);
s 准备好的字符数组,或内存地址
见 7)demo
7)将字符串输出到屏幕
int puts(const char *s);
s 是要输出的字符数组,或内存地址
1 /************************************************************************* 2 > File Name: gets_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 07:50:02 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void) 12 { 13 char str[256] = ""; 14 15 gets(str); 16 // printf("str == %s\n",str); 17 puts(str); 18 return 0; 19 }
8)将字符串按格式写入到内存,或字符数组内
sprintf(char *str,"格式字符串",参数列表....);
str 是准备好的字符数组
格式字符串 由%c %d %lf %f %u等组成的字符串
char str1[256]="";
int a=102;
sprintf(str1,"a=%d, &a=%p, ASCII = %c\n",a,&a,a);
puts(str1);
用途:将数值转换为字符串
1 /************************************************************************* 2 > File Name: sprint_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 09:16:36 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 10 int main(void) 11 { 12 char str[256] = ""; 13 int a = 102; 14 15 sprintf(str,"a = %d,&a = %p,ascii = %c",a,&a,a); 16 // printf("str = %s\n",str); 17 puts(str); 18 return 0; 19 }
9)把字符串转换为整数
#include <stdlib.h>
int atoi(const char *nptr); 把字符串转换为整型
long atol(const char *nptr); 转换为长整型
double atof(const char *nptr);转换为浮点
1 /************************************************************************* 2 > File Name: atoi_test.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Fri 31 Jul 2015 09:29:20 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 #include <stdlib.h> 11 12 int main(void) 13 { 14 char *str = "234"; //if str[] = "abc",after atoi,temp == 0 15 int temp; 16 17 temp = atoi(str); 18 printf("temp == %d\n",temp); 19 return 0; 20 }
b.自定义的字符串处理函数
当涉及到对字符串操作是,一定要记得这几点:
a)若要存储字符串,声明的变量必须指定空间大小,必要是要对其初始化。如 char [256] = "";char *str = "abdefg";
b)操作函数中的参数要用 const 关键子限定,防止外部对存储空间地址进行修改;
1)strcpy_me
1 /************************************************************************* 2 > File Name: strcpy_me.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Sat 01 Aug 2015 03:00:08 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 char *fun_cpy(char *,const char *); 12 13 int main(void) 14 { 15 char *str = "abcdefghikjlmnopqrstuswxyz"; 16 char dest[256] = ""; 17 dest[26] = 'v'; 18 19 fun_cpy(dest,str); 20 printf("dest:%s\n",dest); 21 printf("str :%s\n",str); 22 return 0; 23 } 24 25 26 char *fun_cpy(char *dest,const char *str) 27 { 28 int i = 0; 29 while(*(str + i) != '\0') 30 { 31 *(dest++) = *(str + i); 32 i++; 33 } 34 *dest = '\0'; 35 return dest; 36 }
2)strlen_me
1 /************************************************************************* 2 > File Name: strlen_me.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Sat 01 Aug 2015 03:26:22 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int my_strlen(const char *); 12 13 int main(void) 14 { 15 char *str = "abcdefg"; 16 int val; 17 18 val = my_strlen(str); 19 printf("val == %d\n",val); 20 return 0; 21 } 22 23 int my_strlen(const char *s) 24 { 25 int i = 0; 26 27 while(*(s+i) != '\0') 28 { 29 i++; 30 } 31 return i; 32 }
3)strcat_me
1 /************************************************************************* 2 > File Name: strcat_me.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Sat 01 Aug 2015 04:00:37 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 void my_strcat(char *,const char *); 12 13 int main(void) 14 { 15 char *str = "abcdefg"; 16 char dest[256] = "higklmnl"; 17 18 my_strcat(dest,str); 19 printf("str:%s\n",str); 20 printf("dest:%s\n",dest); 21 return 0; 22 } 23 24 void my_strcat(char *dest,const char *str) 25 { 26 int i; 27 28 while(*dest++) 29 ; 30 dest -= 1; 31 while(*(str+i) != '\0') 32 { 33 *dest++ = *(str+i); 34 i++; 35 } 36 *dest = '\0'; 37 }
4)strcmp_me
1 /************************************************************************* 2 > File Name: strcmp_me.c 3 > Author: shuk 4 > Mail: wsksec@gmail.com 5 > Created Time: Sat 01 Aug 2015 04:12:51 PM CST 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int my_strcmp(const char *,const char *); 12 13 int main(void) 14 { 15 char *str = "abcdefgbra"; 16 char *dest = "abcdefgbaa"; 17 int i; 18 19 i = my_strcmp(dest,str); 20 if(i > 0) 21 { 22 printf("dest > str\n"); 23 } 24 else if(i < 0) 25 { 26 printf("dest < str\n"); 27 } 28 else 29 { 30 printf("dest = str\n"); 31 } 32 33 return 0; 34 } 35 36 int my_strcmp(const char *dest,const char *str) 37 { 38 int i = 0; 39 40 while(1) 41 { 42 if((*(dest + i) == *(str + i)) && (*(dest + i)!='\0')) 43 { 44 i++; 45 } 46 else if(*(dest + i) > *(str + i)) 47 { 48 return 1; 49 } 50 else if(*(dest + i) < *(str + i)) 51 { 52 return -1; 53 } 54 else 55 { 56 return 0; 57 } 58 } 59 }
细节不足的地方请您指教,后续我也会加以说明和修改。