当前位置: 首页 > news >正文

《算法笔记》3.6小节——入门模拟->字符串处理

1009 说反话

在这里插入图片描述

#include <cstdio>

int main() {
    char sen[80][80];
    int num=0;
    while(scanf("%s",sen[num])!=EOF){
        num++;
    }
    for (int i = num-1; i > 0; --i) {
        printf("%s ",sen[i]);
    }
    printf("%s\n",sen[0]);
    return 0;
}

字符串连接

在这里插入图片描述

#include <iostream>
using namespace std;

int main() {
    string a,b;
    while(cin>>a>>b){
        cout<<a<<b<<endl;
    }
    return 0;
}

首字母大写

在这里插入图片描述

#include <iostream>
#include <cctype>
using namespace std;

bool is_blank(char i){
    if(i==' '||i=='\t'||i=='\r'||i=='\n') return true;
    else return false;
}

int main() {
    string str;
    while(getline(cin,str)){
        bool is_start=true;
        for (int i = 0; i < str.length(); ++i) {
            if(is_blank(str[i])) is_start= true;
            else if(is_start){
                if(islower(str[i])){
                    str[i]= toupper(str[i]);
                }
                is_start= false;
            }
        }
        cout<<str<<endl;
    }
    return 0;
}

字符串的查找删除

在这里插入图片描述
在这里插入图片描述
参考链接:

https://cloud.tencent.com/developer/ask/sof/110559246

  • isspace( )判断字符c是否为空白符,空白符指空格、水平制表、垂直制表、换页、回车和换行符
  • 注意大小写比较和输出
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

string removeSpace(const string& input){
    string res;
    for(char c:input){
        if(c!=' ') res.push_back(c);
    }
    return res;
}

int main() {
    string shortStr,inputStr;
    getline(cin,shortStr);
    transform(shortStr.begin(), shortStr.end(), shortStr.begin(), ::tolower);
    while(getline(cin,inputStr)){
        if(shortStr.empty()){
            cout<<inputStr;
            return 0;
        }
        string temp=inputStr;
        transform(temp.begin(),temp.end(),temp.begin(),::tolower);
        size_t pos=0;
        while((pos=temp.find(shortStr,pos))!=string::npos){
            temp.erase(pos,shortStr.length());
            inputStr.erase(pos,shortStr.length());
        }
        string resStr= removeSpace(inputStr);
        cout<<resStr<<endl;
    }
    return 0;
}

单词替换

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str,str1,str2;
    while(getline(cin,str)){
        getline(cin,str1);
        getline(cin,str2);
        size_t pos=0;
        while((pos=str.find(str1,pos))!=string::npos){
            str.replace(pos,str1.length(),str2);
            pos=pos+str2.length();
        }
        cout<<str<<endl;
    }
    return 0;
}

字符串去特定字符

在这里插入图片描述

#include <iostream>
using namespace std;

int main() {
    string s,c;
    while(getline(cin,s)){
        getline(cin,c);
        if(c.empty()){
            cout<<s<<endl;
            continue;
        }
        size_t pos=0;
        while((pos=s.find(c,pos))!= string::npos){
            s.erase(pos,c.length());
        }
        cout<<s<<endl;
    }
    return 0;
}

数组逆置

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    string str;
    while(getline(cin,str)){
        reverse(str.begin(), str.end());
        cout<<str<<endl;
    }
    return 0;
}

比较字符串

在这里插入图片描述

#include <iostream>
using namespace std;

int main() {
    int m;
    string str1,str2;
    cin>>m;
    for (int i = 0; i < m; ++i) {
        cin>>str1>>str2;
        if(str1.length()>str2.length()){
            cout<<str1<<" is longer than "<<str2<<endl;
        }else if(str2.length()>str1.length()){
            cout<<str1<<" is shorter than "<<str2<<endl;
        }else{
            cout<<str1<<" is equal long to "<<str2<<endl;
        }
    }
    return 0;
}

【字符串】回文串

在这里插入图片描述

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    string str;
    getline(cin,str);
    string tempStr=str;
    reverse(tempStr.begin(),tempStr.end());
    if(tempStr==str) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

编排字符串

在这里插入图片描述

#include <iostream>
#include <queue>
#include <string>
using namespace std;

int main() {
    int m;
    cin>>m;
    deque<string> names;
    string str;
    for (int i = 0; i < m; ++i) {
        cin>>str;
        names.push_front(str);
        if(names.size()>4){
            names.pop_back();
        }
        int count=1;
        for(const auto&name:names){
            cout<<count<<'='<<name;
            if(count<names.size()){
                cout<<' ';
            }
            count++;
        }
        cout<<endl;
    }
    return 0;
}

相关文章:

  • 扩散模型 Diffusion Model 整体流程详解
  • 我拿Cursor复现了Manus的效果
  • 上层 Makefile 控制下层 Makefile ---- 第二部分(补充一些例子与细节)
  • URL结构、HTTP协议报文
  • Redis for Windows 后台服务运行
  • 【6】深入学习http模块(万字)-Nodejs开发入门
  • javascript专题2 ---- 在 JavaScript 列表(数组)的第一个位置插入数据
  • 【Linux C】简单bash设计
  • 重返JAVA之路——面向对象
  • 论文:Generalized Category Discovery with Large Language Models in the Loop
  • 玩转ChatGPT:使用深入研究功能梳理思路
  • 最大公约数和最小倍数 java
  • 【Linux实践系列】:匿名管道收尾+完善shell外壳程序
  • redis linux 安装简单教程(redis 3.0.4)
  • Spring Boot(二十一):RedisTemplate的String和Hash类型操作
  • 基于XGBoost的异烟酸生产收率预测:冠军解决方案解析
  • 七大寻址方式
  • ubuntu 系统安装Mysql
  • 【代码安全】spotbugs编写自定义规则(一) 快速开始
  • 【数据可视化艺术·实战篇】视频AI+人流可视化:如何让数据“动”起来?
  • 回家了!子弹库帛书二、三卷将于7月首次面向公众展出
  • 浙江一教师被指殴打并威胁小学生,教育局通报涉事人被行拘
  • 外交部驻港公署正告美政客:威胁恫吓撼动不了中方维护国家安全的决心
  • 第十一届世界雷达展开幕,尖端装备、“大国重器”集中亮相
  • 哈马斯官员:若实现永久停火,可交出加沙地带控制权
  • 澳大利亚首例“漂绿”诉讼开庭:能源巨头因“碳中和”承诺遭起诉