zoukankan      html  css  js  c++  java
  • C++自学笔记_string测试编程题_《C++ Primer》

    今天《C++ Primer》学完第9章,关于string类型,找道题目测试一下效果。
    题目描述:

    输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。

    输入:
    多组数据。每组数据输入包括3行,
    第1行是包含多个单词的字符串 s,
    第2行是待替换的单词a,(长度<=100)
    第3行是a将被替换的单词b。(长度<=100)
     
    s, a, b 最前面和最后面都没有空格.
    输出:
    每个测试数据输出只有 1 行,
    将s中所有单词a替换成b之后的字符串。
    样例输入:
    You want someone to help you
    You
    I
    样例输出:
    I want someone to help you


    用的STL,好爽!!
    Code:
    #include <iostream>
    #include <string>
    #include <cstdio>
     
    using namespace std;
     
    int main()
    {
        string str,word;
        string word_a,word_b;
        while(getline(cin,str)){
            getline(cin,word_a);
            getline(cin,word_b);
            int startPos=0,endPos=0;
            while((startPos=str.find_first_not_of(" ",endPos))!=string::npos){
                endPos=str.find_first_of(" ",startPos);
                word.assign(str,startPos,endPos-startPos);
                if(word==word_a){
                    str.replace(startPos,word_a.size(),word_b);
                }
            }
            cout<<str<<endl;
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1111
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:0 ms
        Memory:1520 kb
    ****************************************************************/
    题目描述:

    对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。
    在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符(' ')、回车符(' ')、换行符(' ')。

    输入:

    输入一行:待处理的字符串(长度小于100)。

    输出:

    可能有多组测试数据,对于每组数据,
    输出一行:转换后的字符串。

    样例输入:
    if so, you already have a google account. you can sign in on the right.
    样例输出:
    If So, You Already Have A Google Account. You Can Sign In On The Right.
    来源:
    2008年北京大学图形实验室计算机研究生机试真题


    没什么好说了,还是STL.
    Code:
    #include <iostream>
    #include <string>
     
    using namespace std;
     
    int main()
    {
        string str;
        string seperators(" ,.	
    ");
        while(getline(cin,str)){
            int startPos=0,endPos=0;
            while((startPos=str.find_first_not_of(seperators,endPos))!=string::npos){
                endPos=str.find_first_of(seperators,startPos);
                if(islower(str[startPos])){
                    str[startPos]=toupper(str[startPos]);
                }
            }
            cout<<str<<endl;
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1121
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:10 ms
        Memory:1520 kb
    ****************************************************************/
    题目描述:

    编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。
    (凡是以一个或多个空格隔开的部分就为一个单词)

    输入:

    输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。

    输出:

    可能有多组测试数据,对于每组数据,
    输出字符串中每个单词包含的字母的个数。

    样例输入:
    hello how are you.
    样例输出:
    5 3 3 3


    Code:
    #include <iostream>
    #include <string>
     
    using namespace std;
     
    int main()
    {
        string str;
        string seperators(" .");
        int wordLength;
        while(getline(cin,str)){
            bool isFirst=true;
            int startPos=0,endPos=0;
            while((startPos=str.find_first_not_of(seperators,endPos))!=string::npos){
                endPos=str.find_first_of(seperators,startPos);
                wordLength=endPos-startPos;
                if(isFirst){
                    cout<<wordLength;
                    isFirst=false;
                }else{
                    cout<<" "<<wordLength;
                }
            }
            cout<<endl;
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1182
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:20 ms
        Memory:1520 kb
    ****************************************************************/
    
    
  • 相关阅读:
    CSS 中 nth-child 和 nth-of-type 的区别
    Git HTTPS 方式自动保存用户名密码
    nginx 配置代理某个路径
    VS Code 常用插件列表
    安装node-sass的正确姿势【转】
    MongoDB 3.4.2 配置 CentOS 6.5 远程连接
    CentOS 6.5 升级 PHP 到5.6
    常用正则表达式整理[JavaScript]
    nginx提示413 Request Entity Too Large解决方法
    Linux远程执行Shell命令或脚本
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4156281.html
Copyright © 2011-2022 走看看