TheKaltur
TheKaltur

Reputation: 21

Conversion of word to alphabetical values of letters c++

I'm just starting with c++ and am trying to write a program, which takes a word and converts the letters to integers matching their position in the alphabet (separated by dots) e.g. hello -> 8.5.12.12.15 (hope I got that one right ;) )

I wrote a little program, but it doesn't seem to work. If I enter a single letter, then the output is correct, but if I enter multiple letters it crashes.

This is the code:

#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;
}

I'm at a loss here and hope if you could help me...

Thanks in advance,

The Kaltur

[edit] fixed code

Upvotes: 0

Views: 2089

Answers (2)

aslg
aslg

Reputation: 1974

Why you're getting an error

You're passing your string as a pointer. And when you use the indexing operator you're not accessing each individual character, you're actually accessing one string in a possible array of strings. Pointers can be treated as an array.

So when you have only letter, the condition

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

is actually ok because you're accessing the first string in an array and are comparing it with another string "a". But when you access the next indices you fall in undefined behaviour as you're accessing invalid memory positions (ie. accessing the second string in an array, but you never intended to make an array of strings).


What you should do instead

There's no need to create a new string. Just writing:

std::string a; // This is enough

And when you pass your string to the function, pass it as a const reference because you're only reading from it,

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

Things you could improve

A for loop is more adequate than a do while for simply iterating an array/string.

In your convert function,

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

There's no return for the case where a character is non-alphabetical.

And you should choose between returning a standalone value,

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

Or returning b at the end,

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

Don't do both.

Notice that you were trying to compare a character with a string.

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

But you actually wanted to compare a character with a character,

a[i] == 'a'

But as others have pointed out, alphabetical characters are linearly indexed so there's a much easier solution than enumerating all possible characters.

Upvotes: 1

therainmaker
therainmaker

Reputation: 4343

The other answers talk about how you can use ASCII values to shorten the code. That is a very reasonable suggestion. Instead of defining values for each letter, you can subtract the value of a - 1 from each character.

However, your query is on why your code isn't working. The main issue is that you are comparing a char with a string. To clarify, there is a difference in "b" and 'b'. When you access an index of a string, the value returned is a character, whereas your if conditions are checking against a string, owing to the use of " instead of '. Fixing this should make your code work. Your code also has an issue that if the character at index i doesn't match any of your if conditions, you code won't return anything. You should look at fixing that as well. If I were to write this code, I would do it as follows:

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

Regarding why your code works for strings of length 1, I can make some guesses, but I would rather prefer that some other knowledgeable members of this community mention that in the comments here.

Upvotes: 1

Related Questions