将单词转换为字母的字母顺序值

Conversion of word to alphabetical values of letters c++

本文关键字:顺序 单词 转换      更新时间:2023-10-16

我刚开始使用c++,我试图写一个程序,它需要一个单词,并将字母转换为与其在字母表中的位置相匹配的整数(用点分隔),例如hello -> 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;
}
我有点不知所措,希望你能帮我……

提前感谢,

Kaltur

[edit] fixed code

为什么会出现错误

你将字符串作为指针传递。当你使用索引运算符的时候你不是在访问每一个单独的字符,你实际上是在访问字符串数组中的一个字符串。指针可以作为数组来处理。

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

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'

但是正如其他人指出的那样,字母字符是线性索引的,所以有一个比列举所有可能的字符更简单的解决方案。

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

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

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

关于为什么你的代码可以为长度为1的字符串工作,我可以做一些猜测,但我更希望这个社区的其他一些知识渊博的成员在这里的评论中提到这一点。