0

The first step, I changed the string to a lowercase, after that I removed all the non letters from the string, now I am struggling to replace each letter with the alphabet position. does anyone know how to do such a thing? Thank you!

string alphabet_position(string message){
   string alphabet= "abcdefghijklmnopqrstuvwxyz";
   int aplha_numbers[100];

 for_each(message.begin(), message.end(), [](char & c){
     c = ::tolower(c);
 });

 for(int i=0; i< message.size(); i++){

     if(message[i] < 'a' || message[i] > 'z'){
         message.erase(i, 1);
         i--;
     }
 }
for(int j=0; j<message.size(); j++){
    int index = alphabet.find(message[j]);
     aplha_numbers[j]= index +1;


}
std::ostringstream os;
for(int z: aplha_numbers){
    os<<z;
}
std::string str(os.str());

return str;
}

Now I have a different issue , I am getting the alphabet positions but I also get a lot of garbage values after the last letter. As an example Input: abc output 123 and after that a lot of numbers 32761004966.....

Mo008
  • 21
  • 4
  • Since there are 26 letters in the English alphabet, replacing characters with their numbers will not be a 1:1 ratio. I recommend creating a new string with the index numbers. See `std::ostringstream`. – Thomas Matthews Apr 18 '22 at 23:53
  • 1
    What's the difficulty level in pasting code as text instead of pictures? I thought it was easy. – Thomas Matthews Apr 18 '22 at 23:54
  • 1
    The number of the letter is given by: `number = letter - 'a';`. – Thomas Matthews Apr 18 '22 at 23:55
  • 1
    @ThomasMatthews — there is nothing in C++ that guarantees that `letter - ‘a’` will work. On a system that uses EBCDIC it will be wrong. For ASCII it works, but unless you know what encoding is being used, getting the position in the alphabet of a letter requires a more sophisticated approach. – Pete Becker Apr 19 '22 at 00:47
  • Hey guys, thank you for your comments, I changed my approach a bit, but I am facing a different issue now. I updated the code. @ThomasMatthews2 – Mo008 Apr 19 '22 at 01:30

1 Answers1

2

There are a few issues in your code:

  1. Your main bug was that in this line:

    for (int z : aplha_numbers)

    You go over all the 100 elements in the allocated array, not just the valid entries. In my solution there's no need for such an array at all. The stringstream objec is updated directly.

  2. The position of a lower case character c is simply c-'a'+1. No need for a lookup table (at least assuming ascii input).

  3. There's no need to actually change the input string by making it a lower case. This can be done on the fly as you traverse it.

Here's a complete fixed version:

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

std::string alphabet_position(std::string message) 
{
    std::ostringstream os;
    for (auto const & c : message)
    {
        char c_lower = std::tolower(c);
        if (c_lower < 'a' || c_lower > 'z')  continue;
        int pos = c_lower - 'a' + 1;
        os << pos;
    }
    return os.str();
}

int main()
{
    std::string s_in = "AbC";
    std::string s_out = alphabet_position(s_in);
    std::cout << "in:" << s_in << ", out:" << s_out << std::endl;
    return 0;
}

Output:

in:AbC, out:123

A side note: it's better to avoid using namespace std;. See here: Why is "using namespace std;" considered bad practice?

wohlstad
  • 12,661
  • 10
  • 26
  • 39