已解决
C语言开发,指针进阶,字符串查找,包含,拼接
来自网友在路上 152852提问 提问时间:2023-10-26 05:16:03阅读次数: 52
最佳答案 问答题库528位专家为你答疑解惑
文章目录
- 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;
}
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"C语言开发,指针进阶,字符串查找,包含,拼接":http://eshow365.cn/6-24798-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!