大写到小写字符串 [英] uppercase to lower case string

查看:94
本文介绍了大写到小写字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用c ++将大写转换为小写.请提供请求的函数.

Hi i want to have code to convert uppercase to lowercase in c++.Please provide the requested function.

推荐答案

尝试以下操作:

Try this:

#include <ctype.h>
void stringtolower(char *input)
{
    for(int i=0;input[i];i++)
        input[i] = tolower(input[i]);
}



Thaddeus使用::tolower()的答案对于基于普通ANSI char的字符串是可以的.

以下内容也适用于非ANSI字符集或基于wchar_t的字符串:
Hi,
Thaddeus''s answer using ::tolower() is OK for plain ANSI char based strings.

The following works as well with non-ANSI character sets or wchar_t based strings:
// Use with VC pre-2010 compilers
#include <string>
#include <locale>
#include <algorithm>
#include <iterator>

template <class C_type>
C_type CharLower(C_type in)
{
    return std::tolower(in, std::locale());
}
template <typename S_type>
S_type ToLower(const S_type& str)
{
    S_type out;
    std::transform(str.begin(), str.end(), std::back_inserter(out), CharLower<S_type::value_type>);
    return out;
}


或由于C ++ 0x部分实现而对于VC2010而言更简单:


or simpler for VC2010 because of C++0x partial implementation:

// Use with VC2010 compilers
#include <string>
#include <locale>
#include <algorithm>
#include <iterator>

template <typename S_type>
S_type ToLower(const S_type& in)
{
    S_type out;
    std::transform(in.begin(), in.end(), std::back_inserter(out), [](S_type::value_type ch){
        return std::tolower(ch, std::locale());});
    return out;
}

您可以使用以下命令测试这两个版本:

You can test both versions with:

#include <iostream>
int main ()
{
    using namespace std;
    string str="Test String.\n";
    cout << ToLower(str) << endl;

    wstring wstr= L"Test String.\n";
    wcout << ToLower(wstr) << endl;

    return 0;
}



欢呼声,
AR



cheers,
AR


这篇关于大写到小写字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆