Chinaunix首页 | 论坛 | 博客
  • 博客访问: 467400
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 13:38:16


点击(此处)折叠或打开

  1. 1.#函数调用情况:
  2.                 1)单独的语句
  3.                 2)表达式的一部分
  4.                 3)作函数的实参
  5. #include <stdio.h>

  6. int max(int a,int b)
  7. {
  8.     return a>b?a:b;
  9. }

  10. int main(int argc, const char *argv[])
  11. {
  12.     printf("hello!\n");
  13.     printf("%d\n",max(3,4));
  14.     return 0;
  15. }
  16. 2.#验证memcpy函数是按字节copy的
  17. #include <stdio.h>
  18. #include <string.h>

  19. int main(int argc, const char *argv[])
  20. {
  21.     int dest[20];
  22.     int src[5]={1,2,3,4,5};
  23.     int i;
  24.     printf("Befor:");
  25.     for (i = 0; i < 5; i++)
  26.     {
  27.         printf("%d ",dest[i]);
  28.     }
  29.     printf("\nAfter:");
  30.     memcpy(dest,src,20);
  31.     for (i = 0; i < 5; i++)
  32.     {
  33.         printf("%d ",dest[i]);
  34.     }
  35.     printf("\n");
  36.     return 0;
  37. }
  38. 3.#验证strncpy:如果n小于src的长度,则目标位置上的内容不会形成一个合法的字符串。
  39. #include <stdio.h>
  40. #include <string.h>
  41. int main(int argc, const char *argv[])
  42. {
  43.     char dest[10]="123456789";
  44.     printf("strncpy(dest,argv[1],atoi(argv[2])):%s\n",strncpy(dest,argv[1],atoi(argv[2])));
  45.     return 0;
  46. }
  47. 4.#验证memset函数:将n个字节填充int c
  48. #include <stdio.h>
  49. #include <string.h>

  50. int main(int argc, const char *argv[])
  51. {
  52.     int num[5]={1,2,3,4,5};
  53.     int i;
  54.     memset(num,0,20);
  55.     for (i = 0; i < 5; i++)
  56.     {
  57.         printf("%d ",num[i]);
  58.     }
  59.     printf("\n");
  60.     return 0;
  61. }
  62. 5.#将num的20个字节的每个二进制位填充为1

  63. #include <stdio.h>
  64. #include <string.h>

  65. int main(int argc, const char *argv[])
  66. {
  67.     int num[5]={1,2,3,4,5};
  68.     int i,j;
  69.     printf("Before:");
  70.     for (i = 0; i < 5; i++)
  71.     {
  72.         printf("%d ",num[i]);
  73.     }
  74.     printf("\nAfter:\n");
  75.     memset(num,0xff,20);
  76.     for (i = 0; i < 5; i++)
  77.     {
  78.         for (j = 31; j >=0; j--)
  79.         {
  80.             if((j+1)%8==0)
  81.                 printf(" ");
  82.             printf("%d",(num[i]>>j)&0x01);
  83.         }
  84.         printf("\n");
  85.     }
  86.     printf("\n");
  87.     return 0;
  88. }
  89. 6.#自定义一个函数,实现memset函数功能
  90. #include <stdio.h>

  91. void *my_memset(void *s,int c,int n)
  92. {
  93.     int i;
  94.     for (i = 0; i < n; i++)
  95.         *((char *)s+i)=c;
  96.     return s;
  97. }

  98. int main(int argc, const char *argv[])
  99. {
  100.     int num[5]={1,2,3,4,5};
  101.     int i;
  102.     printf("Before:");
  103.     for (i = 0; i < 5; i++)
  104.         printf("%d ",num[i]);
  105.     printf("\nAfter :");
  106.     my_memset(num,0,20);
  107.     for (i = 0; i < 5; i++)
  108.         printf("%d ",num[i]);
  109.     printf("\n");
  110.     return 0;
  111. }
  112. 7.#统计子串substr在源串src中出现的次数。源串及子串数据从命令行参数获取。
  113. #include <stdio.h>
  114. #include <string.h>


  115. int main(int argc, const char *argv[])
  116. {
  117.     int time=0;
  118.     char *p;
  119.     while(strstr(argv[1],argv[2]))
  120.     {
  121.         time++;
  122.         argv[1]=strstr(argv[1],argv[2])+strlen(argv[2]);
  123.     }
  124.     printf("strstr_time=%d\n",time);
  125.     return 0;
  126. }
  127. 8.#定义一个字符指针数组如:
  128.     char *str[]={"hello world","hello hell","hello aka","hello hello hoho"};
  129.     从键盘输入字符串,从字符指针数组对应的字符串中查找输入的字符串是否存在,若存在返回该字符串存在指针数组的行列位置。若输入"exit"(不区分大小写)结束字符串输入。
  130. #include <stdio.h>
  131. #include <string.h>

  132. int str_search(char *str[],int n)
  133. {
  134.     int i;
  135.     char s[32];
  136.     while(1)
  137.     {
  138.         printf("enter a string:");
  139.         gets(s);
  140.         if(strcasecmp(s,"exit")==0)
  141.             return -1;
  142.         for (i = 0; i < n; i++)
  143.         {
  144.             char *tmp=str[i];
  145.             while(strstr(tmp,s)!=NULL)
  146.             {
  147.                 printf("%s row:%d\tcol:%d\n",s,i,strstr(tmp,s)-str[i]);
  148.                 tmp=strstr(tmp,s)+strlen(s);
  149.             }
  150.         }
  151.     }
  152. }

  153. int main(int argc, const char *argv[])
  154. {
  155.     char *str[]={"hello world","hello hell","hello aka","hello hello hoho"};
  156.     if(str_search(str,4)<0)
  157.         printf("exit!\n");
  158.     else
  159.         str_search(str,4);
  160.     return 0;
  161. }

阅读(640) | 评论(0) | 转发(2) |
0

上一篇:字符串操作 _0220

下一篇:预处理 _0224

给主人留下些什么吧!~~