C++:vector元素转换大小写

题目

  1. 读取一段文本到vector
  2. 每个单词存储为vector的一个元素.
  3. 把vector中每个单词转化为大写字母.
  4. 输出vector对象中转化后的元素.
  5. 每八个单词为一行输出

分析

  1. 第一,二不解释.
  2. C++里面的toupper(c)和Java里面的toUpperCase()最大的区别是:C++是转换单个字符,而Java的则是转换一个字符串
  3. 后面的不解释了.

C++.
image-1979

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*************************************************************************
    > File Name: test.3.14.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2013年12月22日 星期日 00时52分56秒
 ************************************************************************/

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

int main()
{
    vector<string> message;
    cout << "input message?" << endl;
    string mess ;
    while(cin >> mess){
        message.push_back(mess);
    }
    if(message.size()<7){
        cout << " error , >8 character " << endl;
        return -1;
    }
    for(vector<string>::size_type index=0;index!=message.size();++index){
        for (vector<string>::size_type toup = 0;toup<message[index].size();++toup){    
           
        ((message[index])[toup])=toupper((message[index])[toup]);
        cout <<   ((message[index])[toup]);
        }
        cout << "\t" ;
        if(index==7){
            cout <<  "\n" << endl;
        }
    }
    return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据