【问题标题】:How can I use the toupper or tolower function on a string array如何在字符串数组上使用 toupper 或 tolower 函数
【发布时间】:2020-01-19 19:26:31
【问题描述】:

我想知道如何使用 toupper 功能,以便用户可以以任何他们想要的方式输入这些单词。

    //fill in arrays
     word[0]  =  "VERY";
     word[1]  =  "MERRY";
     word[2]  =  "CHRISTMAS";
     word[3]  =  "EVERYONE";


    for (i= 0; i<4; i++)
    {
        word[i] = toupper(word[i]);
    }

    food[0]  =  "CANDY";
    food[1]  =  "CAKE";
    food[2]  =  "SOUP";
    food[3]  =  "COOKIE";


    for (j=0;j<4;j++)
      {
        food[j] = toupper(food[j]);
     } 


     cout<<"\n Pick a word it can either be VERY, MERRY, CHRISTMAS, EVERYONE (can be written in anyway)";
     cin>> word[i];


     cout<<"\n Pick a food, it can be CAKE,SOUP, CANDY, OR COOKIE, can be written anyway)";
     cin>> food[j];

还有这个错误是什么意思

[Error] no matching function for call to 'toupper(std::string&)'

【问题讨论】:

  • std::toupper 接受 characters(更准确地说是一个字符 int),而不是 std::strings,作为其唯一参数。换句话说,错误消息准确地告诉您问题是什么。
  • 这能回答你的问题吗? Convert a String In C++ To Upper Case
  • 在互联网上搜索“c++ string transform toupper”。
  • 我看了,没那么有用
  • @LolaBenson "我看了没那么有用" 哪方面没用?

标签: c++ arrays string toupper tolower


【解决方案1】:

如果你有一个元素类型为 std::string 的数组,像这样

std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

然后您可以使用基于范围的 for 循环将其所有元素例如转换为小写

#include <string>
#include <cctype>

//…

for ( auto &s : word )
{
    for ( char &c : s ) c = tolower( ( unsigned char )c );
}

这是一个演示程序。

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

int main()
{
    std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

    for ( const auto &s : word ) std::cout << s << ' ';
    std::cout << '\n';

    for ( auto &s : word )
    {
        for ( char &c : s ) c = std::tolower( ( unsigned char )c );
    }

    for ( const auto &s : word ) std::cout << s << ' ';
    std::cout << '\n';

    for ( auto &c : word[0] ) c = std::toupper( ( unsigned char )c );

    for ( const auto &s : word ) std::cout << s << ' ';
    std::cout << '\n';
}

它的输出是

VERY MERRY CHRISTMAS EVERYONE 
very merry christmas everyone 
VERY merry christmas everyone 

或者您可以使用这样的用户定义函数。

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

std::string & change_string_case( std::string &s, bool upper_case = true )
{
    for ( auto &c : s ) c = upper_case ? std::toupper( static_cast<unsigned char>( c ) )
                                       : std::tolower( static_cast<unsigned char>( c ) );

    return s;
}

int main()
{
    std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

    for ( auto &s : word ) std::cout << change_string_case( s, false ) << ' ';
    std::cout << '\n';
}

程序输出是

very merry christmas everyone 

或者您可以编写两个单独的函数,例如

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

std::string & lowercase_string( std::string &s )
{
    for ( auto &c : s ) c = std::tolower( static_cast<unsigned char>( c ) );

    return s;
}

std::string & uppercase_string( std::string &s )
{
    for ( auto &c : s ) c = std::toupper( static_cast<unsigned char>( c ) );

    return s;
}

int main()
{
    std::string word[] =  { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };

    for ( auto &s : word ) std::cout << lowercase_string( s ) << ' ';
    std::cout << '\n';
    for ( auto &s : word ) std::cout << uppercase_string( s ) << ' ';
    std::cout << '\n';
}

程序输出是

very merry christmas everyone 
VERY MERRY CHRISTMAS EVERYONE 

【讨论】:

  • 请考虑 static_cast 而不是 C 风格的演员。
  • @Ayxan 你可以随心所欲。
  • 在这个公开的帖子上,最好能展示一下现代 C++ 是如何做到这一点的。根本不应该使用 C 风格强制转换的原因有很多。
  • 另一种选择是使用std::transform
  • 汽车部分是什么意思?问题是我从来没有学过这种东西,所以它仍然令人困惑
【解决方案2】:

为了自己的理智,我总是将它们包装在方便的函数中:

// uppercase a string
static inline string upper(string s) {
    std::transform(s.begin(), s.end(), s.begin(), [](int ch) { return std::toupper(ch); });
    return s;
}

然后您可以随意应用它们:

for (j=0; j < 4; j++) {
     food[j] = upper(food[j]);
}

假设你有一个字符串数组。

【解决方案3】:

有几种方法可以完成此任务。您可以通过 transform 来自 algorithm 头文件。

#include <algorithm>
void  toUpperCase(std::string& str)
{
     std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}

int main()
{
    std::string str = "hello";
    toUpperCase(&str);
    std::cout << str << std::endl;    // "HELLO WORLD"
}

另一种方法是使用 boost/algorithm/string.hpp 提供的 boost。

#include <boost/algorithm/string.hpp>
#include <string>

int main() {
    std::string str = "Hello World";

    boost::to_upper(str);   
    std::cout << str << std::endl;    // "HELLO WORLD"

    return 0;
}

您可以通过here了解更多详情。

【讨论】:

  • 我看了没用:(
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 2011-03-25
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-13
  • 1970-01-01
相关资源
最近更新 更多