第四章 字符串代码

本文详细介绍了C++中字符串的四种主要操作:构造、操作、运算和函数,并通过实例代码进行演示。包括如何构造字符串、访问和修改字符串内容、执行字符串的加法和比较,以及使用内置函数获取字符串长度和查找特定字符。此外,还展示了两个实际应用示例,分别是特殊乘法遍历和简单密码加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

string的四方面操作:
1.构造
#include  <iostream>
#include  <cstdio>
#include  <cstring>
 
using namespace std;
 
int main(){
 
    string s0 = "Initial String";      //定义字符串s0为Initial String
    string s1;                                 //定义字符串s1
    string s2 (s0);                       //s2拷贝s0所有内容,或者说把s0的值复制给s2
    string s3 (s0,8,3);              //s3拷贝s0从位置8开始的三位数,即Str
    string s4("A character sequence");           
  //定义字符串s4为A character sequence
    string s5("Another character sequence",12);  
  //从位置0开始的12个字符,即Another char
    string s6(10,'x');                        
   //构造十个重复字符,即xxxxxxxxxx
 
    cout << "s0:" << s0 << endl;
    cout << "s1:" << s1 << endl;
    cout << "s2:" << s2 << endl;
    cout << "s3:" << s3 << endl;
    cout << "s4:" << s4 << endl;
    cout << "s5:" << s5 << endl;
    cout << "s6:" << s6 << endl;
   system("pause");
    return 0;
}
2.操作
#include  <iostream>
#include  <cstdio>
#include  <cstring>
 
using namespace std;
 
int main(){
//字符串的访问
    string str = "hello world";
    for(int i = 0;i < str.size(); i++){
        cout << str[i];
    }
 cout << endl << "the 7th element of str is:" << str[6] << endl;
 
//字符串的定义与插入
    string str1 = "to be question";
    string str2 = "that is a ";
    string str3 = "or not world";
    string str4;
 
    str4.insert(0,str1);     
 //从下标0开始插入Str1,即to be question
    str4.insert(6,str3,0,7);  
//从下标6开始插入str3的从0到7,即to be (or not) question,括号内是新加的
    str4.insert(13,"to be "); 
 //从下标13开始插入to be,即to be or not (to be) question
    str4.insert(19,str2);    
 //从下标19插入str2,即to be or not to be (that is a) question
    cout << "str4:" << str4 << endl;
 
//字符串的删除
    str4.erase(19);       //从下标19开始,后面全部删除,即to be or not to be
    str4.erase(0,9);      //从下标0开始删除9个字符,即not to be
    cout << "str4:" << str4 << endl;
 
//字符串的清空
    str4.clear();
    cout << "str4:" << str4 << endl;
 
   system("pause");
    return 0;
}
3.运算
#include  <iostream>
#include  <cstdio>
#include  <cstring>

using namespace std;

int main(){
//字符串的加法
    string str1 = "to be";
    string str2 = "not to be";
    string str3 = "that is a question";
    string str = str1 + ",";      //to be,
    str = str + str2 + ";";        //to be,not to be;
    str += str3;               //to be,not to be;that is a question
    cout << str << endl;

//字符串的大小比较
    str1 = "ab";
    str2 = "abc";
    str3 = "bcc";
    cout << (str1 <= str2) << endl; //true
    cout << (str2 != str3) << endl; //true
    cout << (str3 < str1)  << endl; //false,按照字典序 b>a, c>b
    return 0;
}
4.函数
#include  <iostream>
#include  <cstdio>
#include  <cstring>

using namespace std;

int main(){
    //size返回字符串长度
    string str1 = "Hello World,End World";//21
    int length = str1.size();
    cout << length << endl;

    //find返回字符串的位置
    int position1 = str1.find("World");//6
    int position2 = str1.find("World",10);//16,备注:从下标10开始寻找World位置
    cout << position1 << endl << position2 <<endl;

    position1 = str1.find('l');      //2
    position2 = str1.find('l',10);   //19
    cout << position1 << endl << position2 << endl;

    position1 = str1.find('a');      //-1,找不到字符a返回-1
    cout << position1 << endl;

    //substr返回字符子串
    string str2 = str1.substr(12);     //End World,从下标13开始返回所有子串
    string str3 = str1.substr(12,3);   //End
    cout << str2 << endl << str3 << endl;
    system("pause");
    return 0;
}

1.例题4.1特殊乘法     遍历
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main(){
    string str1,str2;
    while(cin >> str1 >> str2){
        int answer = 0;
        for(int i=0; i<str1.size(); i++){
            for(int j=0; j<str2.size(); j++){
                answer += (str1[i]-'0') * (str2[j]-'0');
             }
        }
            printf("%d\n",answer);
    }
    return 0;
}

 

2.例题4.2简单密码   加密
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main() {
    string str;
    while (getline(cin, str)) {             //起始行
        if (str == "ENDOFINPUT") {
            break;
        }
        getline(cin, str);                  //密文
        for (int i = 0; i < str.size(); ++i) {
            if ('A' <= str[i] && str[i] <= 'Z') {
                str[i] = (str[i] - 'A' - 5 + 26) % 26 + 'A';
            }
        }
        cout << str << endl;
        getline(cin, str);                  //结束行
        return 0;
    }
}

 

 

3. 例题4.3统计字符   统计
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

const int MAXN = 256;

int number[MAXN];

int main() {
    string str1, str2;
    while (getline(cin, str1)) {
        if (str1 == "#") {
            break;
        }
        getline(cin, str2);
        memset(number, 0, sizeof(number));
        for (int i = 0; i < str2.size(); ++i) {
            number[str2[i]]++;
        }
        for (int i = 0; i < str1.size(); ++i) {
            printf("%c %d\n", str1[i], number[str1[i]]);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值