【问题标题】:C++ Changing lower-case letters in a string function (again...)C ++更改字符串函数中的小写字母(再次...)
【发布时间】:2014-01-26 00:36:18
【问题描述】:

又是我。在我所有的问题中,我认为这是所有问题中最愚蠢的,但由于疲劳或愚蠢,我也需要一些帮助。然而,最重要的是,我这样做是为了我的一项任务,并且有一个严格的规则 - 我必须使用一个函数 称为

char* encode(char* source, char const* alpha)

这是我的一段非常原始的代码:

    int len = strlen(source);
    for (int i = 0; i < len; i++)
    {
        switch (source[i])
        {
        case 'a': source[i] = alpha[0];
        case 'b': source[i] = alpha[1];
        case 'c': source[i] = alpha[2];
        ................................
        ................................
        ................................
        case 'y': source[i] = alpha[24];
        case 'z': source[i] = alpha[25];
        default: source[i] = source[i];
        }
    }
    cout << source << endl;

它基本上应该使输入的字符串source不超过1000个符号将其所有小写符号('a' - 'z')更改为已输入数组的对应符号(总共26个符号对于每个小写字母...“a”随 a[0] 变化,“b”随 b[1] 变化,等等)。

我这里有几个问题:

  1. 我的代码不起作用.. 输出总是一些奇怪的符号。我该如何解决?
  2. 如何缩短它?也许使用for 语句而不是switch
  3. 当它工作时,如何实现我在问题开头提到的功能(这可能是最重要的一个)?

仅作记录,这也是我的输入代码:

char source[1001];
cin.getline(source, 1001, '/n');

char alpha[27];
cin.getline(alpha, 27);

编辑:我将代码更改为:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char source[1001];
    cin.getline(source, 1001, '/n');

    char alpha[27];
    cin.getline(alpha, 27);

    const int len = strlen(source);
    for (int i = 0; i < len; i++)
    {
        if ('a' <= source[i] && source[i] <= 'z')
        {
            source[i] = alpha[source[i] - 'a'];
        }
    }
    cout << source << endl;
    return 0;
}

但它变得更加错误。现在我的控制台输入永远不会结束....实际上..单击 Enter 不会停止它..什么都不会..当我将我的 cin.getline for source 更改为 10 它以某种方式结束但又返回了那些奇怪的符号- ╠╠╠╠.

【问题讨论】:

  • switch-case 需要break;
  • 很不清楚,你的问题是什么。到达一个显着点(显示编译器错误、调试结果、意外输出等)
  • @BLUEPIXY 好地方,在所有这些混乱中;) ...
  • 这段代码并不像你想象的那么原始。从'a''z' 的扩展比最初看起来显而易见的用途更多,特别是在'a'..'z'连续的系统上(例如:EBCDIC)。除非您打算包含break stmts,或者向我们展示真实代码,否则自分配是无用的,最后两个分配除外。
  • 去掉最后一个getline()参数。您的意思可能是“\n”(注意正确的斜杠转义字符),但您不需要它,因为换行符是默认终止符。

标签: c++ string function pointers


【解决方案1】:

在 switch 语句中,当匹配一个 case 时,代码将执行所有后续 case,除非您在每个 case 的末尾使用 break。您的代码总是失败并达到默认情况。

【讨论】:

  • 在每种情况下都使用了break;,结果都一样 - 来源:This program is dumb. alpha:qwertyuiopasdfghjklzxcvbnm 输出:T╠╠╠ ╠╠╠╠╠m╠ ╠╠ ╠╠╠
【解决方案2】:

使用cstring 代替string

#include <cstring>

并将switch 替换为if (source[i] &gt;= 'a' &amp;&amp; source[i] &lt;= 'z') ...

这是你的最终代码:https://eval.in/95037

#include <iostream>
#include <cstring>

using namespace std;

void encode(char *source, const char *alpha)
{
    int i, j;
    int len = strlen(source);
    for (i = 0; i < len; i++) {
        if (source[i] >= 'a' && source[i] <= 'z') {
            j = source[i] - 'a';
            source[i] = alpha[j];
        }
    }
}

int main(void)
{
    char source[1001];
    char alpha[27];
    cin.getline(source, 1000);
    cin.getline(alpha, 27);
    encode(source, alpha);
    cout << source << endl;
    return 0;
}

【讨论】:

  • 非常感谢!它可以创造奇迹,但如何将其作为 char* (char* encode(char* source, char const* alpha)) 而不是 void
  • @user3213110 如果要修改原始字符串,为什么要返回char *
【解决方案3】:

你的 switch 语句执行每一步,因为你从来没有breakreturn

更好:

for (int i = 0; i < len; i++) {
    if('a' <= source[i] && source[i] <= 'z') {
        source[i] = alpha[source[i] - 'a'];
    }
}

解释:

source[i] - 'a'0 代表 'a'1 代表 'b' 等等。它概括了您的模式。

【讨论】:

  • 是的,您可以进行“字符数学”,但您需要添加默认操作。一个 else source[i] = source[i] 就足够了。
  • @waTeim, source[i] = source[i] 毫无意义。
  • @Paul Draper 大声笑。 ....需要更多的咖啡;是的,我只是剪切和粘贴,source[i] = source[i] 是不必要的。
  • @PaulDraper 谢谢,这段代码看起来不错。不过,你介意检查我的编辑吗?我已经描述了我遇到的情况。它仍然不起作用。你知道如何为我提到的函数重写它吗?
【解决方案4】:

如果允许,您可以忽略 c 样式字符串 alpha 并使用 cctype 函数 toupper(c)。

#include <cctype>
for (size_t i = 0; i < strlen(source); i++) {
    source[i] = toupper(source[i]);
}

【讨论】:

    【解决方案5】:

    在 C++ 中:

    #include <algorithm>
    #include <string>
        wstring toLower(const wstring& str)
        {
            wstring temp = str;
            std::transform(temp.begin(), temp.end(), temp.begin(), ::towlower);
            return temp;
        }
        //--------------------------------------------------------------------------------------------------------------------- 
        string toLower(const string& str)
        {
            string temp = str;
            std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
            return temp;
        }
    

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 1970-01-01
      • 2020-01-04
      • 2019-06-08
      • 2011-05-12
      • 1970-01-01
      • 2012-01-03
      • 2019-12-24
      • 2021-06-11
      相关资源
      最近更新 更多