【问题标题】:Is there a way to rewrite this? [closed]有没有办法重写这个? [关闭]
【发布时间】:2020-12-17 21:44:29
【问题描述】:
string toLowerCase(string word)
{

  for (int a = 0; a < word.length(); a++)
  {

      if (word[a] >= 'A' && word[a] <= 'Z')
      {

          word[a] = word[a] + 32;
      }
  }


  return word;
}

还有另一种编码方式吗?它不能是 c 字符串。谢谢。

【问题讨论】:

  • 当你尝试自己时,你现在的想法是怎样的?你所看到的有一些系统,不是吗?暗示。空间 (' ') == 32.
  • 我投票结束这个,因为你没有表现出任何努力尝试自己做。将其重写为 what?

标签: c++ string for-loop c++11 visual-c++


【解决方案1】:

以下是一些简化:

for (int a = 0; a < word.length(); a++) 
// can be replaced with
for (auto &letter : word)
if (word[a] >= 'A' && word[a] <= 'Z')
// can be replaced with
if (std::isupper(letter))
word[a] = word[a] + 32;
// can be replaced with
letter = std::tolower(letter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2021-07-27
    • 2018-08-18
    • 2019-10-10
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多