zoukankan      html  css  js  c++  java
  • c++ string 大小写转换,split隔断指定字符

    //问题来源为ACM,将一串字符串缩写变大写,简称。
    //
    这两段代码共用到的头文件 #include<iostream> #include<cstring> #include<cstdio> #include<iostream> #include<iomanip> #include <vector> #include <string> #include <algorithm> using namespace std; //string小写转大写 void toUpper(string &str) { int length = 0; length = str.length(); for(int i=0; i<length; i++) { if(str[i]<='z' && str[i]>='a') str[i] = str[i]-32; } }
    //隔断指定字符
    vector<string> split(string str,string pattern)
    {
        int pos;//std::string::size_type
        vector<string> result;
        str+=pattern;//扩展字符串以方便操作
        int size=str.size();
    
        for(int i=0; i<size; i++)
        {
            pos=str.find(pattern,i);
            if(pos<size)
            {
                string s=str.substr(i,pos-i);
                result.push_back(s);
                i=pos+pattern.size()-1;
            }
        }
        return result;
    }
    
    
    int main()
    {
        string str;
        cin.ignore(1,'
    ');//解决第一次输入多一个回车问题(原因是第一次的回车仍然在输入流中)
        getline(cin,str);
        vector<string> result=split(str," ");//空格分隔单词
        string::size_type i;//因为result.size类型的问题,这里定义i
        for(i=0; i<result.size(); i++)
        {
            result[i]=result[i].substr(0,1);//获得隔断后的首字符
            //transform(result[i].begin(), result[i].end(), result[i].begin(), toupper); //大小写转换,在g++编译器中报错
            toUpper(result[i]);//大小写转换
            cout<<result[i];
        }
        return 0;
    }

    输入:

    most valuable player

    输出:

    MVP

  • 相关阅读:
    PostgreSQL管理工具:pgAdminIII
    PostgresQL7.5(开发版)安装与配置(win2003测试通过)
    让PosggreSQL运行得更好
    在.NET程序中使用PIPE(管道技术)
    在浏览网页过程中,单击超级链接无任何反应
    字符串转换
    数组初始化
    使用现有的COM
    后台服务程序开发模式(一)
    COM的四本好书
  • 原文地址:https://www.cnblogs.com/bkycjj/p/3556267.html
Copyright © 2011-2022 走看看