把 vector 对象中每个单词转化为大写字母_初学vector对象2

读入一段文本到 vector 对象,每个单词存储为 vector 中的一个元素。把 vector 对象中每个单词转化为大写字母。输出 vector 对象中转化后的元素,每八个单词为一行输出。

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

int main()
{
	vector<string> svect;
	string str;

	cout<<"Enter text (Ctrl+z to end) "<<endl;
	while (cin>>str)
	{
		svect.push_back(str);
	}

	if (svect.size()==0)
	{
		cout<<"No String !"<<endl;
		return -1;
	}
	
	for (vector<string>::size_type ix=0;ix!=svect.size();++ix)
	{
		for (string::size_type index=0;index!=svect[ix].size();++index)
		{
			if (islower(svect[ix][index]))
			{
				svect[ix][index]=toupper(svect[ix][index]);//将小写字母转化为大写字母
			}
		}
		cout<<svect[ix]<<'\t';
		if ((ix+1)%8==0)//每行输出八个单词
		{
			cout<<endl;
		}
	}

	return 0;
}


发布了70 篇原创文章 · 获赞 22 · 访问量 42万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章