C++字符串基础

字符转换

eg:将字符转换成数字,将数字转换成字符串

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char c = 'a';
    cout << (int)c << endl;
    cout << (char)97 << endl;
    
    return 0;
}

0-127字符串和数字转换

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    for (int i = 1;i < 128; i++) printf("%d: %c\n", i,(char)i);
    return 0;
}

注:重要的输出结果(数字:字符串)

数字表示字符

48: 0
49: 1
50: 2
51: 3
52: 4
53: 5
54: 6
55: 7
56: 8
57: 9

数字表示大写字母

65: A
66: B
67: C
68: D
69: E
70: F
71: G
72: H
73: I
74: J
75: K
76: L
77: M
78: N
79: O
80: P
81: Q
82: R
83: S
84: T
85: U
86: V
87: W
88: X
89: Y
90: Z

数字表示小写字母

97: a
98: b
99: c
100: d
101: e
102: f
103: g
104: h
105: i
106: j
107: k
108: l
109: m
110: n
111: o
112: p
113: q
114: r
115: s
116: t
117: u
118: v
119: w
120: x
121: y
122: z
大写与小写字母差值是32,详情请参照ASCII字符代码表
在这里插入图片描述

利用字符计算

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{ 
    printf("%d\n", 'b'-'a');
    return 0;
}

得到结果为1

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{ 
    printf("%c\n", 'a' + 3);
    return 0;
}
得到结果是d

理解将char看成整数,当输出时计算机自动转换成字符,字符可以参与运算,运算时将其当作整数。

练习

输入一行字符,统计出其中数字字符的个数,以及字母字符的个数。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char c;
    
    int n = 0 , chars = 0 ;//初始化数字,字符 
    while (cin >> c)//表示读出结束即停止
    {
        if (c >= 48 && c <= '9' ) n ++ ;//表示数字累加
        else if(c >= 65 && c <= 'Z' || c >= 97 && c <= 'z') chars ++;//字母累加
    }
    printf("nums:%d\nchars:%d\n", n, chars);//分别输出数字字母个数
    
    return 0;
}

字符数组

字符串就是字符数组(存储字符)加上结束符’\0‘\0表示结束。每个字符数组的长度至少要比字符串的长度多1。
eg

    char c[] = {'c'};//数组
    char c2[] = {'c','\0'};//字符串数组长度为2
    char c3[] = "c";//等价于c2长度为2 sizeof衡量长度
    

输入输出

输入

注:所有数组的名字本身就是指针不需要&

char s[100];
scanf("%s", s);//不需要& 等价于puts(s);
cout << s << endl ;

读入字符串的时候,cin,scanf遇到’ ‘(空格)就停止读入,此时用fgets
fgets(名字, 最多位数,文件);
eg:

fgets (s, 100, stdin);//cin.getline (s,100);s表示名字,100表示最多的位数。
cout << s << endl;
#include <cstring>
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char s[100];
    fgets(s, 100, stdin);
    
    puts(s);
    
    return 0;
}

string需要用getline函数

string s;
getline (cin, s);

输出

eg

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    
    char c2[] = {'a','b','c','\0'};//字符串数组长度为2
    char c3[] = "c++";//等价于c2长度为2 sizeof衡量长度
    
    cout << c2 + 1 << endl;//可以直接输出,从b开始输出
    printf("%s\n",c3);
    return 0;
}
结果是
bc
c++

数组下标更改位置

eg、

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    
    char s[100];
    cin >> s + 1;//表示数组下标从一开始,原来是0
    cout << s +1 << endl;
     
    cout << s[1] << endl;
    
    return 0;
}

字符数组的正常操作

引入头文件<string.h>< cstring>

strlen

求字符串长度用strlen(str)函数;

#include <cstring>
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char s[100];
    scanf("%s", s);
    cout << strlen(s) << endl;//string length 
    //等价于 int len = 0;for(int i = 0;s1[i];i++ )len++;  调用
    return 0;
}

strcmp

比较字符串大小strcmp(a,b)(比较两个字符串大小a<b返回-1,a ==b返回0,a> b返回1.这里的比较方式是字典序,贪心算法)

#include <cstring>
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char s1[100], s2[100];
    scanf("%s%s", s1, s2);
    cout << strcmp(s1, s2) << endl;//string campare
    return 0;
}
输入abc abc 结果是0


#include <cstring>
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char s1[100];
    scanf("%s", s1);
    cout << strcmp(s1, "abc") << endl;//string campare,输入于abc比较
    return 0;
}
输入abc结果是0

strcpy

字符串s1复制s2

#include <cstring>
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char s1[100],s2[100];
    scanf("%s", s1);//读入s1
    strcpy(s2, s1);//把s1 复制给s2;后面复制给前天一个
    
    cout << s2 << endl;
    return 0;
}

输入abc ef 得出 abc

遍历字符串所有字符

#include <cstring>
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char s1[100];
    scanf("%s", s1);//读入s1
    for (int i = 0; i < strlen(s1); i ++)// i从0开始 遍历到小于其s1长度
    /*
    缩短匹配时间重命名for (int i = 0,len = strlen; i < len(s1); i ++)
    */
        cout << s1[i] << endl;
    return 0;
}

练习

1、给定一个只包含小写字母的字符串,请你找到第一个出现一次的字符,如果没有输出no

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
char str[100010];//限制
int cnt[26];// 存储字符

int main()
{
    cin >> str;//读入数组
    
    int len = strlen(str); //提前调用   
    for (int i = 0; i < len ;i ++) cnt[str[i] - 'a'] ++;//小写字母个数累加读入新数组长度,-a表示排除非字母字符
    for (int i = 0; i < len ;i ++)//统计字符,遍历
        if (cnt[str[i] - 'a'] == 1)//新数组出现1次
        {
            cout << str[i] << endl;
            return 0;
        }    
    puts("no");//输出
    return 0;
}

字符串string 用法

可变长字符数列。

    string s1;//默认的空字符串
    string s2 = s1;//s2是s1的一个副本
    string s3 ="hiya"//s3是该字符串字面值的一个副本
    string s4(10,c)//定义十个c,前面是长度,后面是字符内容

输出字符串,直接用cout

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    string s1, s2;//不可用scanf
    
    cin >> s1 >> s2;
    
    cout << s1 << ' ' << s2 << endl;//printf("%s\n", s1.c_str())
    
    
    return 0;
}

注:读入字符串用cin.getline(s1),string用getline输入完整(cin,s1)输出用puts,

empty()

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string s1 = "abc", s2;//表示定义s1,s2
    
    cout << s1.empty() << endl;
    cout << s2.empty() << endl;//得到0、1是布尔值
    return 0;
}

xx.size()

cout << xx.size ;
返回的是xx字符的长度

比较string

支持">,<,=,<=,>=,==,!=",按字典序进行比较,为string赋值

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    string s1 = "abc", s2;//s1集合为abc
    
    s2 = s1;//让s2=s1
    
    cout << s2 << endl;//输出s2
    return 0;
    
}

累加string

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    string s1 = "abc", s2 = "def";//s1集合为abc
    
    string s3 = s1 + s2;//让s2+s1=s3;也可加字符s3=s3 
    
    cout << s3 << endl;
    return 0;
    
}

当把string对象和字符字面值相加时,至少有一个人是string
string s4 = s1 + " “;//正确
string s5 =” hello" + " "//错误不含有string

处理string中的字符

当成数组处理 每个元素循环一次并输出

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    string s1 = "hello world";
    
    for (int i = 0;i < s1.size();i ++)
    
        cout << s1[i] << endl;
    return 0;
    
}

遍历s1

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    string s1 = "hello world";//每一个数组元素类型
    
    for (char c: s1)//将c替代s1
    
        cout << c << endl;//输出s1,范围遍历增强循环,顺次遍历数组每个元素变量
    return 0;
    
}

替换遍历

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    string s1 = "hello world";//每一个数组元素类型
    
    for (char &c: s1)//将c替代s1
        c = 'a';//把s替换a
    
        cout << s1 << endl;//输出s1,范围遍历增强循环,顺次遍历数组每个元素变量
    return 0;
    
}

### auto遍历自动反馈类型定义

练习

密码翻译;输入一个只包含小写字母的字符串,将其中的每个字母替换成它的后继字母,如果原字母是‘z’替换成‘a’。
加密的规则如下:
字符串中的小写字母,a加密为b,b加密为c,…,y加密为z,z加密为a。
字符串中的大写字母,A加密为B,B加密为C,…,Y加密为Z,Z加密为A。
字符串中的其他字符,不作处理。
请你输出加密后的字符串。
eg
输入样例:
Hello! How are you!
输出样例:
Ifmmp! Ipx bsf zpv!

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    string s;
    getline(cin,s);
    for (auto &c: s)
    {
        if (c >= 'a' && c <= 'z' ) //判断是否是大小写字母,取余使输入字符在区间
            c = (c - 'a' + 1) % 26 + 'a';
        else if (c >= 'A' && c <= 'Z')
            c = (c - 'A' + 1) % 26 + 'A';//取余使输入字符在区间
            
    }
    cout << s << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值