#include<string>
#include<stdio.h>
#include<iostream>
using namespace std;
int main() {
//大小写字符转换
char str[] = "aAdSdSfF";
if (str[8] == '\0')
printf("最后一个字符是\0\n");
for (int i = 0; i < strlen(str);i++) {
str[i] = toupper(str[i]);
}
cout << "转化为大写字母为" << str << endl;
for (int i = 0; i < strlen(str);i++) {
str[i] = tolower(str[i]);
}
cout << "转化为小写字母为" << str << endl;
//判断字符类型
if (isdigit(2))
cout << "1是数字" << endl;
if (isupper('A'))
cout << "A大写字母" << endl;
if (islower('a'))
cout << "a是小写字母" << endl;
//atoi(const char* str):将字符串转化为整形,如果不能转化为整形,返回数字0
char strnum[] = "123456";
long int number = atoi(strnum);
cout << "atoi转化后的效果为" << number<<endl;
//char* strpbrk(const char* str1, const char* str2):在字符串s1中寻找字符串s2中任何一个字符相匹配的第一个字符的位置,空字符NULL不包括在内
char str1[] = "assdfd";
char str2[] = "s";
cout << "str1中的str2第一个匹配的位置为"<<strpbrk(str1, str2)<<endl;
//char* strtok(char* str,const char* sep)
char str3[] = "a,d,ss,f,g,h,";
char* tem;
tem = strtok(str3 , ",");
while (tem != NULL) {
cout << tem<<endl;
tem = strtok(NULL , ",");
}
//char* strstr(const char* str,const char* substr)检索子串在字符串中首次出现的位置
//返回值:返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL
//char* strchr(const char* str, char c)
// 功能:查找字符串中最后一次出现c的位置
// 返回值:返回最后一次出现c的位置的指针,如果s中不存在c则返回NULL
char str5[] = "asasasas";
char* str7 = strchr(str5, 'a');
cout << "第一次出现的位置为" << str7;
cout << "最后一次出现的位置为" << strrchr(str5,'a');
//strcpy
//char* strcpy(char* dest, char* src)
// 1
// 功 能 : 将参数src字符串拷贝至参数dest所指的地址
// 返回值 : 返回参数dest的字符串起始地址
char a[] = "affer";
char b[50];
char* c;
strcpy(b,a);
cout << "复制后的结果为" << b<<endl;
//功能: 字符串拼接
//返回值:返回dest字符串起始地址
//说明:
//- 源字符串必须’\0’结束
//- 目标空间必须可修改
//- strcat() 会将参数src字符串复制到参数dest所指的字符串尾部
//- dest最后的结束字符’\0’会被覆盖掉,并在连接后的字符串的尾部再增加一个’\0’
//- dest与src所指的内存空间不能重叠,且dest要有足够的空间来容纳要复制的字符串
cout << "拼接后的结果为" << strcat(b,a);
}
算法题中对于字符操作的总结
最新推荐文章于 2024-09-02 16:35:28 发布
本文通过C++代码展示了大小写字符的转换、字符类型判断、字符串转整数、字符串查找等功能,包括 toupper(), tolower(), isdigit(), isupper(), islower(), atoi(), strpbrk(), strtok(), strstr(),以及字符串操作如strcpy(), strcat()。
摘要由CSDN通过智能技术生成