hznu.dodo C++ 实验六 字符串

1.

【描述】
判断输入的一个字符串是否为回文串,若是输出“Yes”,否则输出“No”。回文串是指正读和反读都一样的字符串,如level。
【输入】
输入一个字符串。
【输出】
输出“Yes”或“No”。
【输入示例】

abcddcba

【输出示例】

Yes
#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
using namespace std;

int main(){
    string str;
    cin >> str;
    string reverse_str = string(str.rbegin(),str.rend());
    if(str == reverse_str) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

2.

【描述】
输入一个字符串,统计并输出该字符串中26个英文字母(不区分大小写)出现的次数。
【输入】
输入一个字符串。
【输出】
分行输出26个英文字母(不区分大小写)出现的次数。
【输入示例】

I am a student.

【输出示例】

a:2
d:1
e:1
i:1
m:1
n:1
s:1
t:2
u:1
#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;

int main(){
    string str;
    getline(cin,str);
    map<char,int> letter_count;
    for(char ch : str){
        ch = tolower(ch);
        if(isalpha(ch)) letter_count[ch]++;
    }
    for(char ch = 'a';ch <= 'z';ch++){
        if(letter_count[ch] > 0) cout << ch << ":" << letter_count[ch] << endl;
    }
    return 0;
}

3.

【描述】
输入5个字符串,输出其中最大的字符串(按照字典顺序)。
【输入】
输入5个字符串。
【输出】
输出5个字符串中最大的字符串。
【输入示例】

red
blue
yellow
green
purple

【输出示例】

yellow

#include<iostream>
#include<cmath>
#include<string>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;


int main(){
    string str_arr[5];
    for(int i=0;i<5;i++){
        cin >> str_arr[i];
    }
    string max_str = str_arr[0];
    for (int i = 1; i < 5; i++)
    {
        if(str_arr[i] > max_str) max_str = str_arr[i];
    }
    cout << max_str;
    return 0;
}

4.【描述】
定义和调用函数:bool isAnagram(string str1, string str2),检查两个单词是否是字母易位词,如果是,返回true;否则返回false。两个单词如果包含相同的字母,次序不同,则称为字母易位词(anagram)。例如,“silent”和“listen”是字母易位词。
【输入】
输入有两行,分别对应两个单词。
【输出】
若两个单词是字母易位词,输出true,否则输出false。
【输入示例】

silent
listen

【输出示例】

true
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool isAnagram(string str1, string str2);
int main() {
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);
    cout << boolalpha << isAnagram(str1, str2) << endl;
    return 0;
}
/* 请在下面编写isAnagram函数 */
bool isAnagram(string str1,string str2){
    if(str1.length() != str2.length()) return false;
    sort(str1.begin(),str1.end());
    sort(str2.begin(),str2.end());
    return str1 == str2;
}

5.

【描述】
输入一个字符串,求出其中最长的英文单词的长度,并输出。单词之间只能用空格间隔。
【输入】
输入一个字符串。
【输出】
输出字符串中最长的英文单词的长度。
【输入示例】

Nice to meet you

【输出示例】

4

#include<iostream>
#include<cmath>
#include<string>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
#include<cstdlib>
using namespace std;


int main(){
    string str;
    getline(cin,str);
    istringstream iss(str);
    string word;
    int max_length = 0;
    while(iss >> word){
        if(word.length() > max_length) max_length = word.length();
    }
    cout << max_length;
    return 0;
}

6.【描述】
输入5个字符串,按字典顺序升序排序后输出。
【输入】
输入5个字符串。
【输出】
输出升序排序后的5个字符串。
【输入示例】

red
blue
yellow
green
purple

【输出示例】

blue
green
purple
red
yellow
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
#include<cstdlib>
using namespace std;



int main(){
    vector<string> str_arr(5);
    for(int i=0;i<5;i++){
        cin >> str_arr[i];
    }
    sort(str_arr.begin(),str_arr.end());
    for(const auto& str : str_arr){
        cout << str << endl;
    }
    return 0;
}

7.【描述】
输入一段英文,计算并输出该段英文中元音字母的个数,不区分大小写。
【输入】
一行中输入一段英文。
【输出】
一行中输出5个数字,分别为字母a、e、i、o、u的个数,数字以空格间隔。
【输入示例】

The C++ programming language is a general-purpose computer programming language originally developed in 1983.
【输出示例】
9 10 6 6 4
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
#include<cstdlib>
using namespace std;



int main(){
    string str;
    getline(cin,str);
    int count[5] = {0};
    for(char ch : str){
        ch = tolower(ch);
        if(ch=='a') count[0]++;
        if(ch=='e') count[1]++;
        if(ch=='i') count[2]++;
        if(ch=='o') count[3]++;
        if(ch=='u') count[4]++;
    }
    for(int i=0;i<5;i++){
        cout << count[i] << " ";
    }
    return 0;
}

8.

【描述】
编写程序,提取一个字符串中的所有数字字符('0'…'9'),将其转换为一个整数输出。
【输入】
在一行中,输入一个字符串。
【输出】
在一行中输出转换后的整数。题目保证输出不超过长长整型范围。
【输入示例】

free82jeep5

【输出示例】

825
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
#include<cstdlib>
using namespace std;



int main(){
    string str;
    getline(cin,str);
    string digits;
    for(char ch : str){
        if(isdigit(ch)) digits += ch;
    }
    long long ans = stoll(digits);
    cout << ans;
    return 0;
}

9.

【描述】
每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔符后的五位数字代表该书在该出版社的编号;最后一位为识别码。
识别码的计算方法如下: 
首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,...,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。 
你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出你认为是正确的ISBN号码。
【输入】
输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN的格式要求)。
【输出】
输出只有一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。
【输入示例】

0-123-41562-4


【输出示例】

Right
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
#include<cstdlib>
using namespace std;



int main(){
    string str;
    getline(cin,str);
    string digits;
    for(char ch : str){
        if(isdigit(ch)) digits += ch;
    }
    int sum = 0;
    if(str[str.length()-1] == 'X'){
        for(int i=0;i<digits.length();i++){
            sum += int(digits[i] - '0') * (i+1);
        }
    }else{
        for(int i=0;i<digits.length()-1;i++){
            sum += int(digits[i] - '0') * (i+1);
        }
    }
    //cout << str[str.length()-1] << endl;
    if(str[str.length()-1]!= 'X' &&  sum % 11 == int(digits[digits.length()-1] - '0')) cout << "Right";
    else if(str[str.length()-1] == 'X'){
        if(sum % 11 == 10) cout << "Right";
        else{
            str[str.length()-1] = char(sum % 11 + '0');
            cout << str;
        }
    }else if(str[str.length()-1]!= 'X' &&  sum % 11 != int(digits[digits.length()-1] - '0')){
        if(sum % 11 == 10){
            str[str.length()-1] = 'X';
            cout << str;
        }else{
            str[str.length()-1] = char(sum % 11 + '0');
            cout << str;
        }        
    }
    
    return 0;
}


10.【描述】
输入一段英文,将该段英文中的每一个单词翻转后输出。
【输入】
一行中输入一段英文。单词之间以空格隔开。
【输出】
一行中输出翻转每一个单词后的字符串
【输入示例】
hello world
【输出示例】
olleh dlrow

#include<iostream>
#include<cmath>
#include<string>
#include<sstream>
#include<vector>
#include<array>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;

string reverse(const string& word){
    return string(word.rbegin(),word.rend());
}
int main(){
    string str;
    getline(cin,str);
    string word;
    string result;
    istringstream iss(str);
    while(iss >> word){
        result += reverse(word) + " ";
    }
    if(!result.empty()) result.pop_back();
    cout << result << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值