【问题标题】:Capitalize function not working properly大写功能无法正常工作
【发布时间】:2017-06-24 05:04:50
【问题描述】:

我正在学习 C++ 的基础知识,并且正在尝试编写一个简单的函数,将给定输入中每个单词的每个字母大写。我写的:

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

int main()
{
    std::cout << "Please enter a sentence: ";
    std::vector<std::string> words;
    std::string x;

    while (std::cin >> x) {
        words.push_back((std::string) x);
    }
    std::cout << std::endl;
    std::vector<std::string>::size_type size;
    size = words.size();

    for (int j = 0; j != size; j++) {
        std::string &r = words[j];
        for (int i = 0; i != r.length(); i++) {
            r = toupper(r[i]);
            std::cout << r << std::endl;
        }
    }
}

返回每个单词的首字母大写。比如我写hello world,程序返回:

H
W

谁能告诉我我做错了什么以及如何解决它。

【问题讨论】:

  • 删除 (std::string) 演员 - 它什么都不做。
  • 我正在尝试编写一个简单的函数,将给定输入中每个单词的每个字母都大写 -- 如果您考虑学习算法函数“C++ 基础”,您可以简单地使用 std::transform(words[j].begin(), words[j].end(), words[j].begin(), toupper); 而不是 i 循环。

标签: c++ function capitalize toupper


【解决方案1】:
for (int j = 0; j != size; j++) {
    std::string &r = words[j];
    for (int i = 0; i != r.length(); i++) {
        r = toupper(r[i]);
        std::cout << r << std::endl;
    }
}

r = toupper(r[i]);,您将r 覆盖为长度为1 的字符串。因此您的内部for 循环条件变为假,您退出了内部循环。所以只打印出每个单词的首字母。

要解决此问题,请将 toupper 的返回值保存到其他变量。

for (int j = 0; j != size; j++) {
    std::string &r = words[j];
    for (int i = 0; i != r.length(); i++) {
        char c = toupper(r[i]);
        std::cout << c << std::endl;
    }
}

【讨论】:

    【解决方案2】:

    你对每个单词的处理都是错误的:

        for (int i = 0; i != r.length(); i++) {
            r = toupper(r[i]);
            std::cout << r << std::endl;
        }
    

    你真正需要的是只修改第一个字母:

        r[0] = toupper(r[0]);
        std::cout << r << '\n';
    

    为了简化,你的循环:

    std::vector<std::string>::size_type size;
    size = words.size();
    for (int j = 0; j != size; j++) {
        std::string &r = words[j];
    

    可以更简洁:

    for (std::string &r : words) {
    

    【讨论】:

      【解决方案3】:

      我有一个 Utility 类,它只包含 static 用于执行字符串操作的函数或方法。这是我的班级使用toUppertoLower 静态方法的样子:

      实用程序

      #ifndef UTILITY_H
      #define UTILITY_H
      
      #include <string>
      
      class Utility {
      public:
          static std::string toUpper( const std::string& str );
          static std::string toLower( const std::string& str );
      private:
          Utility();
      };
      
      #endif // UTILITY_H
      

      #include "Utility.h"
      #include <algorithm>
      
      std::string Utility::toUpper( const std::string& str ) {
          std::string result = str;
          std::transform( str.begin(), str.end(), result.begin(), ::toupper );
          return result;
      }
      
      std::string Utility::toLower( const std::string& str ) {
          std::string result = str;
          std::transform( str.begin(), str.end(), result::begin(), ::tolower );
          return result;
      }
      

      用法:

      #include <string>
      #include <iostream>
      
      #include "Utility.h"
      
      int main() {
          std::string strMixedCase = std::string( "hEllO WOrlD" );
          std::string lower = Utility::toLower( strMixedCase );
          std::string upper = Utility::toUpper( strMixedCase );
      
          std::cout << lower << std::endl;
          std::cout << upper << std::endl;
      
          return 0;
      }
      

      注意: - 这会对传入的字符串进行完整的字符串操作。如果您尝试在字符串中执行特定字符;您可能需要做一些不同的事情,但这是如何使用 &lt;algorithm&gt;'s std::transform()::toupper::tolower 的开始

      【讨论】:

        猜你喜欢
        • 2017-09-23
        • 2013-11-06
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多