C语言-------字符串库函数的使用
来源:互联网 发布:福建广电网络集团副总 编辑:程序博客网 时间:2024/05/21 17:30
- 1.#函数调用情况:
- 1)单独的语句
- 2)表达式的一部分
- 3)作函数的实参
- #include <stdio.h>
- int max(int a,int b)
- {
- return a>b?a:b;
- }
- int main(int argc, const char *argv[])
- {
- printf("hello!\n");
- printf("%d\n",max(3,4));
- return 0;
- }
- 2.#验证memcpy函数是按字节copy的
- #include <stdio.h>
- #include <string.h>
- int main(int argc, const char *argv[])
- {
- int dest[20];
- int src[5]={1,2,3,4,5};
- int i;
- printf("Befor:");
- for (i = 0; i < 5; i++)
- {
- printf("%d ",dest[i]);
- }
- printf("\nAfter:");
- memcpy(dest,src,20);
- for (i = 0; i < 5; i++)
- {
- printf("%d ",dest[i]);
- }
- printf("\n");
- return 0;
- }
- 3.#验证strncpy:如果n小于src的长度,则目标位置上的内容不会形成一个合法的字符串。
- #include <stdio.h>
- #include <string.h>
- int main(int argc, const char *argv[])
- {
- char dest[10]="123456789";
- printf("strncpy(dest,argv[1],atoi(argv[2])):%s\n",strncpy(dest,argv[1],atoi(argv[2])));
- return 0;
- }
- 4.#验证memset函数:将n个字节填充int c
- #include <stdio.h>
- #include <string.h>
- int main(int argc, const char *argv[])
- {
- int num[5]={1,2,3,4,5};
- int i;
- memset(num,0,20);
- for (i = 0; i < 5; i++)
- {
- printf("%d ",num[i]);
- }
- printf("\n");
- return 0;
- }
- 5.#将num的20个字节的每个二进制位填充为1
- #include <stdio.h>
- #include <string.h>
- int main(int argc, const char *argv[])
- {
- int num[5]={1,2,3,4,5};
- int i,j;
- printf("Before:");
- for (i = 0; i < 5; i++)
- {
- printf("%d ",num[i]);
- }
- printf("\nAfter:\n");
- memset(num,0xff,20);
- for (i = 0; i < 5; i++)
- {
- for (j = 31; j >=0; j--)
- {
- if((j+1)%8==0)
- printf(" ");
- printf("%d",(num[i]>>j)&0x01);
- }
- printf("\n");
- }
- printf("\n");
- return 0;
- }
- 6.#自定义一个函数,实现memset函数功能
- #include <stdio.h>
- void *my_memset(void *s,int c,int n)
- {
- int i;
- for (i = 0; i < n; i++)
- *((char *)s+i)=c;
- return s;
- }
- int main(int argc, const char *argv[])
- {
- int num[5]={1,2,3,4,5};
- int i;
- printf("Before:");
- for (i = 0; i < 5; i++)
- printf("%d ",num[i]);
- printf("\nAfter :");
- my_memset(num,0,20);
- for (i = 0; i < 5; i++)
- printf("%d ",num[i]);
- printf("\n");
- return 0;
- }
- 7.#统计子串substr在源串src中出现的次数。源串及子串数据从命令行参数获取。
- #include <stdio.h>
- #include <string.h>
- int main(int argc, const char *argv[])
- {
- int time=0;
- char *p;
- while(strstr(argv[1],argv[2]))
- {
- time++;
- argv[1]=strstr(argv[1],argv[2])+strlen(argv[2]);
- }
- printf("strstr_time=%d\n",time);
- return 0;
- }
- 8.#定义一个字符指针数组如:
- char *str[]={"hello world","hello hell","hello aka","hello hello hoho"};
- 从键盘输入字符串,从字符指针数组对应的字符串中查找输入的字符串是否存在,若存在返回该字符串存在指针数组的行列位置。若输入"exit"(不区分大小写)结束字符串输入。
- #include <stdio.h>
- #include <string.h>
- int str_search(char *str[],int n)
- {
- int i;
- char s[32];
- while(1)
- {
- printf("enter a string:");
- gets(s);
- if(strcasecmp(s,"exit")==0)
- return -1;
- for (i = 0; i < n; i++)
- {
- char *tmp=str[i];
- while(strstr(tmp,s)!=NULL)
- {
- printf("%s row:%d\tcol:%d\n",s,i,strstr(tmp,s)-str[i]);
- tmp=strstr(tmp,s)+strlen(s);
- }
- }
- }
- }
- int main(int argc, const char *argv[])
- {
- char *str[]={"hello world","hello hell","hello aka","hello hello hoho"};
- if(str_search(str,4)<0)
- printf("exit!\n");
- else
- str_search(str,4);
- return 0;
- }