C语言开发,指针进阶,字符串查找,包含,拼接

编程入门 行业动态 更新时间:2024-10-28 08:31:32

C语言开发,指针<a href=https://www.elefans.com/category/jswz/34/1769503.html style=进阶,字符串查找,包含,拼接"/>

C语言开发,指针进阶,字符串查找,包含,拼接

文章目录

  • C语言开发,指针进阶。
    • 1.字符串与指针的关系
    • 2.指针获取字符串具体内容
    • 3.字符串比较,查找,包含,拼接
    • 4.字符串大小写

C语言开发,指针进阶。

1.字符串与指针的关系

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>int main11() {char str[] = {'d', '1', 's','\0'};//str数组的字符串是存在全局区,静态区域,修改的时候拷贝一份到main函数的栈区,再进行修改操作str[1] = 'a';//printf遇到\0结束printf("%s\n", str);//开辟内存地址,指向静态区域里面的aaaa,指针不能修改静态全局区域内的字符串内容,char *str2 = "aaaa";//崩溃str2[1] = '2';return 0;
}

2.指针获取字符串具体内容

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>int main11() {char str[] = {'d', '1', 's','\0'};//str数组的字符串是存在全局区,静态区域,修改的时候拷贝一份到main函数的栈区,再进行修改操作str[1] = 'a';//printf遇到\0结束printf("%s\n", str);//开辟内存地址,指向静态区域里面的aaaa,指针不能修改静态全局区域内的字符串内容,char *str2 = "aaaa";//崩溃str2[1] = '2';return 0;
}
//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>int getLen(char *string) {int cnt = 0;//移动指针 ,不等于'\0',一直循环,while (*string) {string++;cnt++;}return cnt;
}//数组作为参数传递,会把数组优化成指针
void getLen2(int *res, int arr[]) {/* int len = sizeof(arr) / sizeof(int);printf("%d\n", len);*/int cnt = 0;while (*arr) {arr++;cnt++;}*res = cnt;
}int main() {char str[] = {'d', '1', 's', '\0'};int len = getLen(str);printf("%d\n", len);int arr[] = {1, 2, 3, '\0'};int len2 = sizeof(arr) / sizeof(int);int res;getLen2(&res, arr);printf("%d\n", len2);return 0;
}

3.字符串比较,查找,包含,拼接

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main() {//字符串转换char *aaa = "1222";int res = atoi(aaa);//true,不等于0,if (res) {printf("%d\n", res);} else {printf("----");}//doubledouble res2 = atof(aaa);printf("%lf\n", res);//字符串比较char *string1 = "aaa";char *string2 = "aaasss";//区分大小写int ress = strcmp(string1, string2);//不区分大小写int resss = stricmp(string1, string2);//0是相等,非0不相等if (!ress) {printf("yes");} else {printf("no");}return 0;
}
//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main() {char *aa = "1234";char *aaa = "1";char *test = strstr(aa, aaa);//非NULL,进ifif (test) {printf("%s\n", test);} else {}//包含if (test) {} else {}//取位置int index = test - aa;//拼接字符串char test1[22];char *a1 = "11", *a2 = "22", *a3 = "33";//先拷贝到数组容器中,再拼接字符串strcpy(test1, a1);strcat(test1, a2);strcat(test1, a3);return 0;
}

4.字符串大小写

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>void low(char *b, char *c) {while (*c) {*b = tolower(*c);//移动指针c++;//一边移动一边存储。b++;}*b = '\0';
}int main() {//都变成小写char *a = "qDHaAA";//char test[20];low(test, a);return 0;
}

更多推荐

C语言开发,指针进阶,字符串查找,包含,拼接

本文发布于:2023-12-03 09:36:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1653841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进阶   字符串   指针   语言

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!