【问题标题】:lower string characters and add a _ in front of converted capital letter降低字符串字符并在转换后的大写字母前添加 _
【发布时间】:2014-02-26 01:11:12
【问题描述】:

我还有一个问题,我想在每个大写字母前加一个_,它会被转换为小写,而且第一个字母不能是大写!我不知道该怎么做...:{示例:

输入:loLollL,输出:lo_loll_l 我也希望它倒退:输入:lo_loll_l 输出:loLollL

代码在这里:

#include <iostream>
#include <algorithm>

using namespace std;

int main ()
{
    const int max = 100;
    string slovo;
    int pocet_r;

    cout << "Zadaj pocet uloh:" << endl;
    cin >> pocet_r;

    if(pocet_r >= 1 && pocet_r <=100)
 {

     // funkcia na zabezpecenie minimalneho poctu chars
          for (int i = 0; i <pocet_r; i++)
     {
           cout << "Uloha " << i+1 << ":" << endl; 

                cin >> slovo;

                if(slovo.size() > max)
                {
                 cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
                }
                 while( slovo.size() > max) 
                 {
                  cin >> slovo;
                 }      

                 for (int i=0; i <= slovo.size(); i++)
                 {
                   int s = slovo[i];
                   while (s > 'A' && s <= 'Z')
                   {
                      if(s<='Z' && s>='A'){
                      return s-('Z'-'_z');
                      }else{

                      cout <<  "chyba";

                      }
                   } 


                }


           cout << slovo[i] << endl;   

     }   

 }else{
     cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}

编辑>

for (int i=0; i <= slovo.size(); i++)
            {
                while (slovo[i] >= 'A' && slovo[i] <= 'Z')
                {
                      string s = transform(slovo[i]);

    cout << s << endl;

    s = untransform(s);

    cout << s << endl;
}
                      }

【问题讨论】:

  • 1.反向结果不是唯一定义的,即使 _ 在原始字符串中被禁止,lo_loll_l 也可以转换为 loLollLLoLollL。 2.'_z'不是字符,这是错字吗?
  • 3.为什么在代码中间从main 返回?这将结束程序。 4. 变量名和输出文本看不懂。如果您将它们全部翻译成英文,这会更容易。 5. 在某些时候你应该分配给新的字符串,你永远不会那样做。
  • 我更新了我的代码应该做什么,字符串不能以大写字母开头
  • 我正在学习 C++,所以有什么问题,请告诉我,告诉我它应该是什么样子,请……我处理了几个小时:(
  • 既然你没有说你的代码有什么问题,我投票结束作为Wrong answer for code to convert between Java camel case and C++ underscore identifiers的副本,这应该会引导你得到一个正确的答案。

标签: c++ algorithm tolower


【解决方案1】:

这应该可行:

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

using namespace std;

string
transform(const string& s)
{
    const size_t n = s.size();
    string t;

    for (size_t i = 0; i < n; ++i)
    {
        const char c = s[i];

        if (isupper(c))
        {
            t.push_back('_');
        }

        t.push_back(tolower(c));
    }

    return t;
}

string
untransform(const string& s)
{
    string t;

    const size_t n = s.size();
    size_t i = 0;

    while (i < n)
    {
        char c = s[i++];

        if (c != '_')
        {
            t.push_back(c);
            continue;
        }

        c = s[i++];

        t.push_back(toupper(c));
    }

    return t;
}

int
main()
{
    string s = transform("loLollL");

    cout << s << endl;

    s = untransform(s);

    cout << s << endl;
}

【讨论】:

  • 谢谢,所以现在我必须把它放在我的代码的标题中,然后把其余的放到主函数中?
  • @feri 您可以按原样获取transformuntransform 的代码(+ 附加的#include 指令)。然后调整您的 main 函数以调用这些函数以将转换应用于您的程序从 cin 读取的字符串。
  • 我更新了我的代码,但调用转换时出现错误
  • @feri transform 必须在整个字符串 slovo 上调用,而不是单个字符。
  • 是的,我现在知道了,但它的作用是它会立即向后显示它,我不想要它,我想在用户向后输入输入时向后显示它,所以如果我输入: lolKO,我的输出是:lol_k_o,但是当我输入 lol_k_o 时,我得到 lolKO 输出,不是马上,你明白吗?非常感谢!
猜你喜欢
  • 2021-01-06
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多