[c/c++源码] 新手学习字符串控制string.h头文件用法和函数

[复制链接]
查看479 | 回复0 | 2022-1-3 01:43:25 | 显示全部楼层 |阅读模式
本帖最后由 破走论坛 于 2022-1-3 01:44 编辑
  1. //1.strcat字符串拼接函数
  2. void main(void){
  3.     char dest[50];
  4.     char *a = "china";
  5.     char *b = " is powerful!";
  6.     strcpy(dest,a);
  7.     strcat(dest,b);
  8.     printf("%s\n",dest);

  9.     system("pause");
  10. }

  11. //2.strchr在一个串中查找给定字符的第一个匹配之处
  12. void main(void){
  13.     char *str = "I want go to USA!";
  14.     //U元素的指针
  15.     //str+3
  16.     char* p = strchr(str,'w');
  17.     if (p){
  18.         printf("索引位置:%d\n", p - str);
  19.     }
  20.     else{
  21.         printf("没有找到");
  22.     }

  23.     system("pause");
  24. }

  25. //3.strstr 从字符串haystack中寻找needle第一次出现的位置
  26. void main(void){
  27.     char *haystack = "I want go to USA!";
  28.     char *needle = "to";
  29.     //U元素的指针
  30.      
  31.     char* p = strstr(haystack, needle);
  32.     if (p){
  33.         printf("索引位置:%d\n", p - haystack);
  34.     }
  35.     else{
  36.         printf("没有找到");
  37.     }

  38.     system("pause");
  39. }

  40. //4.strcmp 比较字符串 strcmpi 比较字符串,忽略大小写
  41. void main(void){
  42.     char *str1 = "abc";
  43.     char *str2 = "ABC";
  44.     //int r = strcmpi(str1, str2);
  45.     int r = _strcmpi(str1, str2);
  46.     printf("%d\n",r);
  47.     //str1 > str2
  48.     if (r > 0){
  49.         printf("str1 大于str2\n");
  50.     }
  51.     else if (r == 0){
  52.         printf("str1 等于str2\n");
  53.     }
  54.     //str1 < str2
  55.     else if (r < 0){
  56.         printf("str1 小于str2\n");
  57.     }

  58.     system("pause");
  59. }

  60. //5.strset 把字符串s中的所有字符都设置成字符c
  61. void main(void){
  62.     char str[] = "internet change the world!";
  63.     _strset(str,'w');
  64.     printf("%s\n",str);
  65.     system("pause");
  66. }

  67. //6.strrev 把字符串s的所有字符的顺序颠倒过来
  68. void main(void){
  69.     char str[] = "internet change the world!";
  70.     _strrev(str);
  71.     printf("%s\n", str);
  72.     system("pause");
  73. }

  74. //7.atoi 字符串转为int类型、atol():将字符串转换为长整型值
  75. void main(void){
  76.     char* str = "a78";
  77.     //int r = atoi(str);   
  78.     printf("%d\n", r);
  79.     system("pause");
  80. }

  81. //8.字符串转为double类型
  82. void main(void){
  83.     char* str = "77b8b";
  84.     char** p = NULL;
  85.     //char* p = str + 2;
  86.     //参数说明:str为要转换的字符串,endstr 为第一个不能转换的字符的指针
  87.     double r = strtod(str,p);
  88.     printf("%lf\n", r);
  89.     printf("%#x\n", p);
  90.     system("pause");
  91. }

  92. //9.strupr转换为大写
  93. void main(void){
  94.     char str[] = "CHINA motherland!";
  95.     _strupr(str);
  96.     printf("%s\n",str);
  97.     system("pause");
  98. }

  99. //10.转换为小写
  100. void mystrlwr(char str[],int len)
  101. {
  102.     int i = 0;
  103.     for (; i < len; i++)
  104. {
  105.         //A-Z 字母 a-Z
  106.         if (str[i] >= 'A' && str[i] <= 'Z')
  107. {
  108.             str[i] = str[i]-'A' + 'a';
  109.         }
  110.     }
  111. }

  112. void main(void){
  113.     char str[] = "CHINA motherland!";
  114.     mystrlwr(str,strlen(str));
  115.     printf("%s\n", str);
  116.     system("pause");
  117. }

  118. //11.练习:删除字符串中指定的字符
  119. void delchar(char *str, char del){
  120.     char *p = str;
  121.     while (*str != '\0') {
  122.         if (*str != del) {
  123.             *p++ = *str;
  124.         }
  125.         str++;
  126.     }
  127.     *p = '\0';
  128. }
  129. //删除最后一个字符
  130. int main()
  131. {
  132.     char str[] = "vencent ppqq";
  133.     delchar(str,'t');
  134.     printf("%s\n", str);
  135.     system("pause");
  136. }
  137. //Java String replaceAll
  138. //StringBuffer buff.deleteCharAt(buff.length()-1);
  139. //删除最后一个字符
  140. void main(void){
  141.     char str[] = "internet,";
  142.     str[strlen(str) - 1] = '\0';
  143.     printf("%s\n", str);
  144.     //作业:realloc实现StringBuffer的拼接,而不是一开始开辟一个很大的数组
  145.     //结构体StringBuffer
  146.     system("pause");
  147. }

  148. //12.memcpy 由src所指内存区域复制count个字节到dest所指内存区域
  149. void main(void){
  150.     char src[] = "C,C++,Java";
  151.     char dest[20] = {0};
  152.     //字节
  153.     memcpy(dest,src,5);
  154.     printf("%s\n",dest);
  155.     system("pause");
  156. }

  157. //13.memchr 从buf所指内存区域的前count个字节查找字符ch。
  158. void main(void){
  159.     char src[] = "C,C++,Java";
  160.     char ch = 'C';
  161.     //字节 (分段截取)
  162.     char* p = memchr(src+3, ch, 5);
  163.     if (p){
  164.         printf("索引:%d\n", p - src);
  165.     }
  166.     else{
  167.         printf("找不到
  168. ");
  169.     }
  170.     system("pause");
  171. }

  172. //14.memmove 由src所指内存区域复制count个字节到dest所指内存区域。
  173. void main(){
  174.     char s[] = "Michael Jackson!";
  175.     //截取的效果
  176.     memmove(s, s + 8, strlen(s) - 8 - 1);
  177.     s[strlen(s) - 8] = 0;
  178.     printf("%s\n", s);
  179.     getchar();
  180. }

  181. //14.在字符串s1中寻找字符串s2中任何一个字符相匹配的第一个字符的位置,空字符NULL不包括在内
  182. void main(){
  183.     char *s1 = "Welcome To Beijing";
  184.     char *s2 = "to";
  185.     char *p;

  186.     p = strpbrk(s1, s2);
  187.     if (p)
  188.         printf("%s\n", p);
  189.     else
  190.         printf("Not Found!\n");

  191.     p = strpbrk(s1, "Da");
  192.     if (p)
  193.         printf("%s", p);
  194.     else
  195.         printf("Not Found!");

  196.     getchar();
  197. }
复制代码

快速回复换一批
遇见神贴岂能不顶
好贴帮顶
强无敌
马克一下
楼主好人一生平安
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则