c++小写字母转化为大写字母

#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main(){
vector<string> svec;
string str;
cout<<"Enter text ctrl(z to end):"<<endl;
while(cin>>str)
svec.push_back(str);
if(svec.size()==0){
cout<<"no string"<<endl;
return -1;}
cout<<"transform element from the vector"<<endl;
for(vector<string>::size_type ix=0;ix!=svec.size();++ix)
{
for(string::size_type index=0;index!=svec[ix].size();++index)
if(islower(svec[ix][index]))
svec[ix][index]=toupper(svec[ix][index]);
cout<<svec[ix]<<" ";
if((ix+1)%==8)
cout<<endl;
}
return 0;
}
在for语句之前可以运行,后面就不行了,什么地方出问题了?
改为
if((ix+1)%8==0)
之后,
可以运行,但是不输出结果?????????????

CNnumen911所说的去掉while之后是可以运行,
但题目是:要求读入一段文本到vector对象,每个单词存储为vector中的一个元素。要求把每个单词换成大写字母,每8个单词为一行。
(去掉while后读入的只是一个对象,而vecort可以定义对象的集合)

由于while(cin>>str)的作用,所以输入要等到文件结束才会终止,因此,当输入完所有要输入的字符串之后要新起一行,并按CRTL+Z(或者LINUX系统中是CTRL+D)之后再回车才会有输出.

***************************************************

if((ix+1)%==8)

应改为

if((ix+1)%8==0)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-03-14
C中有个函数可以直接转换,char * strupr(char *s);
需要加入头文件#include <string.h>
第2个回答  2010-03-14
/*改好了, 试试吧*/

#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main(){
vector<string> svec;
string str;
cout<<"Enter text ctrl(z to end):"<<endl;
cin>>str; //这里为什么要用while()循环呢? 改成这样试试吧...
svec.push_back(str);
if(svec.size()==0){
cout<<"no string"<<endl;
return -1;}
cout<<"transform element from the vector"<<endl;
for(vector<string>::size_type ix=0;ix!=svec.size();++ix)
{
for(string::size_type index=0;index!=svec[ix].size();++index)
if(islower(svec[ix][index]))
svec[ix][index]=toupper(svec[ix][index]);
cout<<svec[ix]<<" ";
if((ix+1)%8 == 0)
cout<<endl;
}
return 0;
}
第3个回答  2021-03-12

信息学奥赛 1138-将字符串中的小写字母转换成大写字母

相似回答