第四章 字符串代码

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所有内容
    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:" << s1 << endl;
    cout << "s3:" << s1 << endl;
    cout << "s4:" << s1 << endl;
    cout << "s5:" << s1 << endl;
    cout << "s6:" << s1 << 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(3,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;
    string += 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 == "ENDOFIPUT"){
            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;

int number[128];

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]]);
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值