注册 登录
编程论坛 C++教室

把输入string 里的小写字母变成大写字母问题(部分解决)

沿途有鬼 发布于 2008-07-28 17:03, 5391 次点击
//把输入string 里的小写字母变成大写字母

#include<iostream>
#include<string>

using namespace std;

void toup(string & str);

int main()
{
    string st;
    cout<<"Enter astring (q to quit): ";
    
    while(getline(cin,st))
    {
        cout<<"Enter astring (q to quit): ";
       
        if(islower(str))
            toup(st);
        cout<<st;
    }
    return 0;
}

void toup(string & str)
{
    str=toupper(str);
}

我程序写的有问题,请大家帮我修改正确,如果可以指点下错在哪里更感谢啦~

[[it] 本帖最后由 沿途有鬼 于 2008-8-5 10:27 编辑 [/it]]
15 回复
#2
zerocn2008-07-28 19:17
if(islower(str))//这个打错了吧st
#3
Fancylovingu2008-07-28 19:28
函数用错了吧!
islower()的参数好像不是字符串吧,是字符吧。至于toupper我忘了怎么用了,不过应该可以用于字符串。你试一下,应该是islower()的问题.
#4
很远的那颗星2008-07-28 23:09
自已写一个啦,还用的着调用库函数......
#5
liuliu20032008-07-29 11:22
saa
void toup(string & str)
{
    str=strupr(str);
}
#6
很远的那颗星2008-07-29 12:39
没啥事做,就写一个吧,小写换大写转换,可处理任意字符,ctrl + Z 结束输入.
#include<iostream>
using namespace std;
int main()
{
    char c;
    while ((cin>>c) != '\0')
    {
        if (c>='a' && c<='z') {c -= 32; cout<<c;}
        else cout<<c;
    }
    return 0;
}


[[it] 本帖最后由 很远的那颗星 于 2008-7-29 12:40 编辑 [/it]]
#7
hank_wh2008-07-29 23:45
toupper() 函数只能转换单个字符,而且你要用的话先要包含ctype.h这个头文件
#8
mark02892008-08-01 16:47
6楼的 Ctrl+Z 结束输入是怎么做到的?

刚学C++,Ctrl+Z 是C++固有的结束输入方式吗?

请指教
#9
很远的那颗星2008-08-02 12:13
Ctrl + Z 不是C++ 固有的结束输入方式.
#10
mark02892008-08-02 16:09
难道ctrl+Z代表字符'\0'?
#11
沿途有鬼2008-08-05 10:27
谢谢各位的回答,虽然还是不是很明白~
#12
zerocn2008-08-06 21:56
islower函数用不对,toupper函数也用不对,请参考MSDN

一下的程序能实现你的功能
#include<iostream>
#include <string>
#include <stdlib.h>

using namespace std;

void Toupper(string &);

int main()
{
    string st("abc");
    cout<<"Enter astring (q to quit): ";
   
   
    while(getline(cin,st))
    {   if(st=="q")
        break;
        cout<<"Enter astring (q to quit): ";
        Toupper(st);
        cout<<st;
    }
    system("pause");
    return 0;
}

void Toupper(string &str)
{  
    for(int i=0;i<str.size();i++)
    if(str[i]>='a' && str[i]<='z')
    str[i]-=32;
}
#13
Vegertar2008-08-07 17:27
大写小写全转了
# include <iostream>
# include <string>
# include <cctype>

void to_lower(std::string& s) //转小写
{
    for(int i = 0; i < s.size(); ++i)
         s[i] = tolower( s[i] );
}

void to_upper(std::string& s)  //转大写
{
    for(int i = 0; i < s.size(); ++i)
         s[i] = toupper( s[i] );
}

int main()
{
    std::string s;
    std::cout << "input a string : ";
    std::cin >> s;

    to_lower(s);
    std::cout << s << std::endl;
    to_upper(s);
    std::cout << s << std::endl;

    return 0;
}
#14
细雨斜飞2008-08-10 15:17
你的  q 跳出循环写在哪里????? 楼主 我没看到啊???
#15
elegant872008-08-11 21:36
我给你写了一个,你看看吧!
这里用到的函数toupper(c);它的功能是如果c是小写的字母,则将小写转换为大写。否则直接返回c。
记住用这个函数需要#include<cctype>头文件的!

//把输入string 里的小写字母变成大写字母

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
    string st;
    cout<<"Enter a string (ctrl+z to quit): ";
    while(getline(cin,st))
    {
       string::size_type ix;
       for(ix=0;ix!=st.size();++ix)
              st[ix]=toupper(st[ix]);
       cout<<"\nChange big word result is: ";
       cout<<st<<endl;
       cout<<"\nEnter a string (ctrl+z to quit): ";
    }
    system("pause");
    return 0;
}
#16
xlh52252008-08-11 21:38
有我么困难么?还部分解决,夸张的很
1