【问题标题】:Conversion of word to alphabetical values of letters c++将单词转换为字母c ++的字母值
【发布时间】:2015-11-24 14:25:09
【问题描述】:

我刚开始使用 c++ 并正在尝试编写一个程序,该程序接受一个单词并将字母转换为与其在字母表中的位置相匹配的整数(由点分隔),例如你好-> 8.5.12.12.15(希望我猜对了;))

我写了一个小程序,但它似乎不起作用。如果我输入一个字母,那么输出是正确的,但是如果我输入多个字母,它就会崩溃。

这是代码:

#include "stdafx.h"
#include <iostream>
#include <string>

int convert(std::string* a, int i)
{
    int b;
    if (a[i] == "a") { b = 1; return b;}
    else if (a[i] == "b") { b = 2; return b;}
    else if (a[i] == "c") { b = 3; return b;}
    else if (a[i] == "d") { b = 4; return b;}
    else if (a[i] == "e") { b = 5; return b;}
    else if (a[i] == "f") { b = 6; return b;}
    else if (a[i] == "g") { b = 7; return b;}
    else if (a[i] == "h") { b = 8; return b;}
    else if (a[i] == "i") { b = 9; return b;}
    else if (a[i] == "j") { b = 10; return b;}
    else if (a[i] == "k") { b = 11; return b;}
    else if (a[i] == "l") { b = 12; return b;}
    else if (a[i] == "m") { b = 13; return b;}
    else if (a[i] == "n") { b = 14; return b;}
    else if (a[i] == "o") { b = 15; return b;}
    else if (a[i] == "p") { b = 16; return b;}
    else if (a[i] == "q") { b = 17; return b;}
    else if (a[i] == "r") { b = 18; return b;}
    else if (a[i] == "s") { b = 19; return b;}
    else if (a[i] == "t") { b = 20; return b;}
    else if (a[i] == "u") { b = 21; return b;}
    else if (a[i] == "v") { b = 22; return b;}
    else if (a[i] == "w") { b = 23; return b;}
    else if (a[i] == "x") { b = 24; return b;}
    else if (a[i] == "y") { b = 25; return b;}
    else if (a[i] == "z") { b = 26; return b;}
}

int main()
{
    std::string* a = new std::string;
    std::string out;
    std::cout << "Please enter a word: ";
    std::cin >> *a;
    int i = 0;
    do
    {
        out += std::to_string(convert(a, i)) + ".";
        i++;
    } while (i < a->size());
    std::cout << "The converted word is: " << out << std::endl;
    return 0;
}

我在这里不知所措,希望你能帮助我......

提前致谢,

卡图尔

[编辑] 固定代码

【问题讨论】:

    标签: c++ string word letters alphabet


    【解决方案1】:

    为什么会出现错误

    您将字符串作为指针传递。当您使用索引运算符时,您并没有访问每个单独的字符,您实际上是在访问可能的字符串数组中的一个字符串。指针可以被视为一个数组。

    所以当你只有字母时,条件

    a[0] == "a"   // is the same as *a == "a"
    

    实际上没问题,因为您正在访问数组中的第一个字符串并将其与另一个字符串"a" 进行比较。 但是当您访问下一个索引时,您会陷入未定义的行为,因为您正在访问无效的内存位置(即访问数组中的第二个字符串,但您从未打算创建一个字符串数组)。


    你应该怎么做

    无需创建new 字符串。随便写:

    std::string a; // This is enough
    

    当您将字符串传递给函数时,将其作为 const 引用传递,因为您只是从中读取数据,

    int convert( const std::string &s, int i );
    

    您可以改进的地方

    for 循环比 do while 更适合简单地迭代数组/字符串。

    在您的 convert 函数中,

    int b;
    if (a[i] == 'a') { b = 1; return b;}
    else if (a[i] == 'b') { b = 2; return b;}
    ...
    

    对于非字母字符的情况没有返回值。

    你应该选择返回一个独立的值,

    if (a[i] == 'a') { return 1;}
    else if (a[i] == 'b') { return 2;}
    ...
    

    或者最后返回b,

    int b = -1; // Say -1 for a non alphabetical character
    if (a[i] == 'a') { b = 1; }
    else if (a[i] == 'b') { b = 2; }
    ...
    return b; // Return ONLY at the end
    

    不要两者都做。

    请注意,您正在尝试将字符与字符串进行比较。

    a[i] == "a" // In your code this is comparing a string with a string
    // But if you pass your string correctly then it would be character-string comparison
    

    但你实际上想将一个角色与一个角色进行比较,

    a[i] == 'a'
    

    但正如其他人所指出的,字母字符是线性索引的,因此有一个比枚举所有可能的字符更容易的解决方案。

    【讨论】:

    • 感谢您的回答,非常有帮助:)
    【解决方案2】:

    其他答案讨论了如何使用 ASCII 值来缩短代码。这是一个非常合理的建议。您可以从每个字符中减去a - 1 的值,而不是为每个字母定义值。

    但是,您的查询是关于您的代码为何不起作用。主要问题是您将charstring 进行比较。澄清一下,"b"'b' 是有区别的。当您访问字符串的索引时,返回的值是一个字符,而您的if 条件正在检查string,因为使用" 而不是'。解决这个问题应该使您的代码工作。您的代码还有一个问题,如果索引 i 处的字符与您的任何 if 条件不匹配,您的代码将不会返回任何内容。你也应该考虑解决这个问题。如果我要写这段代码,我会这样做:

    int convert(std::string &a, int i)
    {
      return (a[i] - 'a' + 1);
    }
    

    关于为什么您的代码适用于长度为1 的字符串,我可以做出一些猜测,但我更希望该社区的其他一些知识渊博的成员在这里的 cmets 中提及这一点。

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 2013-02-08
      • 2013-02-04
      相关资源
      最近更新 更多