C++大小写字母转换问题

imdulier 2011-04-05 08:06:00
怎样提示用户从键盘输入一句话(其中可能包含空格,以EOF标记作为结束,即用户输入Ctrl+Z作为输入结束),将其中的大写字母换成对应的小写字母,小写字母换成对应的大写字母。
...全文
1845 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
笨蛋糕 2013-04-07
  • 打赏
  • 举报
回复
笨蛋糕 2013-04-07
  • 打赏
  • 举报
回复
想问问楼上,该用什么库函数呢?
zjs100901 2013-04-06
  • 打赏
  • 举报
回复
引用 21 楼 wangyaqi123 的回复:
[quote=引用 10 楼 sanshao1314 的回复:]用个库函数就是了
++[/quote]++
无间虚者 2013-04-02
  • 打赏
  • 举报
回复
引用 10 楼 sanshao1314 的回复:
用个库函数就是了
++
zhengjingc 2013-04-02
  • 打赏
  • 举报
回复
引用 19 楼 qq120848369 的回复:
C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#include <iostream>#include <string>#includ……
虽然事隔两年,但是想问一下,如果输入要去换行要怎么办,好像用getline就不行了,是吗。那要用什么输入格式呢。
a102010 2011-04-06
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
using namespace std;
void main()
{
string s;
cout<<"Input s:\n";
cin>>s;
size_t i;
for(i=0; i<s.length() ;i++) {
if(s[i]>='a' && s[i]<='z')
{
s[i]=s[i]^32;
}
else
{
s[i]=s[i]|32;
}
}
cout<<s;
}
p95635746 2011-04-06
  • 打赏
  • 举报
回复

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

int main()
{
cout<<"Input a sentence: (press ctrl+z to end)" << endl;
string str;
while(getline(cin, str))
{
for(string::size_type index = 0; index != str.size(); ++index)
{
if(islower(str[index]))
{
str[index] = toupper(str[index]);
}
else if(isupper(str[index]))
{
str[index] = tolower(str[index]);
}
}
cout << str << endl;
}
cin.clear();
return 0;
}


LZ试试这个吧,我在自己的机器上测试通过了,一楼的条件表达式有点BUG,要是输入的句子中有标点符号什么的话...就会有问题。
qq120848369 2011-04-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string convert(const string &line)
{
string findBullShit=line,copyLine=line;

for(string::size_type i=0;i!=findBullShit.length();++i)
{
if( 'a'<=findBullShit[i] && findBullShit[i]<='z' || 'A'<=findBullShit[i] && findBullShit[i]<='Z')
{
findBullShit[i]|=32; //全部转化成小写
}
}

string::size_type pos=0;

while( ( pos=findBullShit.find("bullshit",pos) ) != string::npos ) //替换不良词汇
{
//findBullShit.replace(pos,strlen("bullshit"),"bush");
copyLine.replace(pos,strlen("bullshit"),"BUSH");
++pos;
}

for(string::size_type i=0;i!=copyLine.size();++i) //逆转大小写
{
if('a'<=copyLine[i] && copyLine[i]<='z')
{
copyLine[i]&=~32;
}
else if('A'<=copyLine[i] && copyLine[i]<='Z')
{
copyLine[i]|=32;
}
}

//pos=0;

/*
while( ( pos=copyLine.find("BUSH",pos) ) != string::npos ) //逆转"BUSH"到"bush"
{
copyLine.replace(pos,strlen("BUSH"),"bush");
}
*/

string::size_type i,j;

for(i=-1,j=0;j!=copyLine.size();++j) //去除多余空格
{
if(i!=-1 && copyLine[i]==' ' && copyLine[j]==' ')
{
continue;
}

copyLine[++i]=copyLine[j];
}

return copyLine.substr(0,i+1); //返回结果
}

int main()
{
string input;

getline(cin,input);
cout<<convert(input)<<endl;

return 0;
}


修正了所有BUG,优化了设计,欢迎品尝.
qq120848369 2011-04-06
  • 打赏
  • 举报
回复
    
while( ( pos=findBullShit.find("bullshit",pos) ) != string::npos ) //替换不良词汇
{
//findBullShit.replace(pos,strlen("bullshit"),"bush");
copyLine.replace(pos,strlen("bullshit"),"bush");
}


这个可以注释掉.
qq120848369 2011-04-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string convert(const string &line)
{
string findBullShit=line,copyLine=line;

for(string::size_type i=0;i!=findBullShit.length();++i)
{
if( 'a'<=findBullShit[i] && findBullShit[i]<='z' || 'A'<=findBullShit[i] && findBullShit[i]<='Z')
{
findBullShit[i]|=32; //全部转化成小写
}
}

string::size_type pos=0;

while( ( pos=findBullShit.find("bullshit",pos) ) != string::npos ) //替换不良词汇
{
findBullShit.replace(pos,strlen("bullshit"),"bush");
copyLine.replace(pos,strlen("bullshit"),"bush");
}

for(string::size_type i=0;i!=copyLine.size();++i) //逆转大小写
{
if('a'<=copyLine[i] && copyLine[i]<='z')
{
copyLine[i]&=~32;
}
else if('A'<=copyLine[i] && copyLine[i]<='Z')
{
copyLine[i]|=32;
}
}

pos=0;

while( ( pos=copyLine.find("BUSH",pos) ) != string::npos ) //逆转"BUSH"到"bush"
{
copyLine.replace(pos,strlen("BUSH"),"bush");
}

string::size_type i,j;

for(i=-1,j=0;j!=copyLine.size();++j) //去除多余空格
{
if(i!=-1 && copyLine[i]==' ' && copyLine[j]==' ')
{
continue;
}

copyLine[++i]=copyLine[j];
}

return copyLine.substr(0,i+1); //返回结果
}

int main()
{
string input;

getline(cin,input);
cout<<convert(input)<<endl;

return 0;
}


buLLShIt , heLLo 123 !!~ buul, bull shit,shitBuLLLl,bullshit,bB
bush , HEllO 123 !!~ BUUL, BULL SHIT,SHITbUlllL,bush,Bb
Ping_QC 2011-04-06
  • 打赏
  • 举报
回复
悲剧,c用多了不会用cpp了

#include <stdio.h>
int main(){
char str[1000];
int i;
while(gets(str)){
for(i = 0; str[i]; i++){
if(str[i] >= 'a' && str[i] <= 'z')
printf("%c",str[i] - 32);
else if(str[i] >= 'A' && str[i] <= 'Z')
printf("%c",str[i] + 32);
else
putchar(str[i]);
}
putchar(10);
}
return 0;
}
zjs100901 2011-04-06
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 a102010 的回复:]
#include<iostream>
#include<string>
using namespace std;
void main()
{
……
s[i]=s[i]|32;
……
[/Quote]
输入[]试试。
delphiwcdj 2011-04-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 imdulier 的回复:]

关键是后面的bullshit(不论大小写)转换成其他字符不知道用什么函数,要怎样识别一连串的的字符呢?还有多余的空白符也不懂得怎样识别.
[/Quote]
转换大小写的方法,上面的朋友们已经给你参考了
替换用下面的方法

string str1, str2("bullshit");
getline(cin, str1);// 可以输入空白符
string::size_type pos = str1.find(str2, 0);// 返回bullshit在str1中的下标pos
str1.replace(pos, strlen("bullshit"), "bush");// 将bullshit替换为bush,并返回str1的引用
hk173 2011-04-05
  • 打赏
  • 举报
回复
ascii码
Freedom 2011-04-05
  • 打赏
  • 举报
回复
用个库函数就是了
imdulier 2011-04-05
  • 打赏
  • 举报
回复
关键是后面的bullshit(不论大小写)转换成其他字符不知道用什么函数,要怎样识别一连串的的字符呢?还有多余的空白符也不懂得怎样识别.
imdulier 2011-04-05
  • 打赏
  • 举报
回复
问题补充,再次请教各位:提示用户从键盘输入一句话(其中可能包含空格,以EOF标记作为结束,即用户输入Ctrl+Z作为输入结束),将其中的大写字母换成对应的小写字母,小写字母换成对应的大写字母。为避免不雅字眼,还需要将其中的“bullshit”(不论大小写)替换为“bush”(全部小写)。最后将替换前后的两句话分别显示在屏幕上。 要求:
 使用string对象和应用于string对象的函数;
 在输出替换前后的文字时需去除原输入语句中多余的空白符(即多于一个的连续的空白符)。
囧囧囧1024 2011-04-05
  • 打赏
  • 举报
回复

#include<string>
#include<iostream>
using namespace std;
void main()
{
string s;
cout<<"Input s:\n";
cin>>s;
size_t i;
for(i=0; i<s.length() ;i++)
{
if(s[i]>='a' && s[i]<='z')
{
s[i]=s[i]^32;
}
else
{
s[i]=s[i]|32;
}
}
cout<<s;
}

sylcc_ 2011-04-05
  • 打赏
  • 举报
回复
#include<string>
#include<iostream>
void main()
{
string s,sum;
while(getline(cin,s))
sum=sum+s;
for(string::size_type index = 0;index != sum.size();++index)
sum[index] =toupper(sum[index]); //toupper(s)如果s是小写转换为大写,否则为s
cout<<sum<<endl;
}
無_1024 2011-04-05
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
string str;
while(getline(cin,str))
{
for(int i = 0;i < str.length(); ++i )
{
if(str[i] >= 'a' && str[i] <= 'z' )
{
str[i] = str[i] - 32;
}
else if( str[i] >= 'A' && str[i] <= 'Z' )
{
str[i] = str[i] + 32;
}
}
cout << str << endl;
}
return 0;
}

加载更多回复(4)

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧