-2

The gist of it is that every letter from a-z needs to be encrypted into a number.

For example a will turn to "1", b into "2" all the way to z="26". Then I have to guess the number of possible outcomes for every encryption. For example 25114 can be 6 different thing. It can be BEAN,BEAAD,YAAD,YAN,YKD,BEKD.

My question is "How do I do this" ? I've tried using if but it keeps printing "1" as an output every time.

#include <iostream>

using namespace std;

int main()
{
    int a1,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

    cout<<"vnesi kod"<<endl;
    cin>>a1;
    if (a)
    {
        cout<<"1"<<endl;
    }
    else if (b)
    {
        cout<<"2"endl;
    }
    return 0;
}
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • you mean `Encoded` probably. – John Boker Jan 06 '20 at 20:47
  • 2
    The question can be translated as: _Given a string of `n` characters/numbers, in how many ways can I divide it into groups of 1 or 2 characters without any group representing a number larger than 26_ – kvantour Jan 06 '20 at 20:48
  • `int a1,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;` doesn't seem to me to be the right way to go about this. Consider using a structure that maps letters to numbers (or if you can be guaranteed that the letters are contiguous and ordered in your character encoding, a bit of math). – user4581301 Jan 06 '20 at 20:48
  • 1
    To begin with you need to get the digits one by one or two by two from the number you input. And where do you initialize `a`? What do you think `if(a)` would do? – Some programmer dude Jan 06 '20 at 20:49
  • You can add spaces and commas. You can always write two digit numbers. You can write one ascii character. You can encode in various formats used to send binary data by email or http interfaces. The real question is, what have you tried? – Kenny Ostrom Jan 06 '20 at 20:50
  • 1
    @kvantour The OP says "I've tried using "if" but it keeps printing "1" as an output every time." which seems to indicate that this might be an actual attempt. – Some programmer dude Jan 06 '20 at 20:52
  • 2
    To the poster: Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please create a [mcve] of your actual attempt (if the code shown isn't it). – Some programmer dude Jan 06 '20 at 20:52
  • 2
    Hi OP, if the code shown *is* your actual attempt at solving this then I'm pretty sure that it would be best for you to try to find a tutor / a good book (booklist: https://stackoverflow.com/q/388242/1116364) because it's - IMO - unlikely that this question can be edited into a form that's both useful for you and the community at large – Daniel Jour Jan 06 '20 at 22:02

2 Answers2

2

Since this is a homework problem, I just give you some pseudo-code on how to solve this. You will still have to implement it yourself.

Let us assume you get a number as input existing out of n digits: a1a2a3 ... an

Since the alphabet contains 26 letters, we want to split this number into groups of 1 or 2 digits and if we have a group of two digits, you have to check if the number is smaller than 27. The quickest way to do this is to make use of a recursive function. It is not the cleanest, but the quickest. Let us assume the recursive function is called decode.

It is very easy to understand why a recursive function is needed. If we want to decode the number 25114. There are two paths we need to take, groups of 1 and groups of 2:

  • group of 1: translate the last digit 4 into "D", and decode the remaining number 2511
  • group of 2: check if the last two digits are smaller than 27, translate the last two digits 14 into N and decode the remaining number 251

In pseudo-code this looks like this:

# function decode
#   input: the number n to decode
#          a postfix string p representing the decoded part
function decode(n, p) {
    # end condition: If the number is ZERO, I have decoded the full number
    #                only print and return
    if (n == 0) { print p; return }
    # group of 1: use integer division to extract the
    #             last digit as n%10 and
    #             remainder to decode is n/10
    decode(n/10, concat(translate(n%10),p) )
    # group of 2: use integer division to extract the
    #             last two digits as n%100 and
    #             remainder to decode is n/100
    # This does not need to run if n < 10 or if n%100 > 26
    if (n > 9 && n%100 <= 26) { decode(n/100, concat(translate(n%100),p) ) }
}
  • The function concat concatenates two strings: concat("AA","BB") returns "AABB"
  • The function translate(n) converts a number n into its corresponding alphabetic character. This can be written as sprintf("%c",64+n)

As is mentioned in the comments, this is not a very efficient method. This is because we do the same work over and over. If the input reads 25114, we will do the following steps in order:

step 1: translate(4), decode _2511_
  step 1.1: translate(1), decode _251_
     step 1.1.1: ...
  step 1.2: translate(11), decode _25_
     step 1.2.1: ...
step 2: translate(14), decode _251_

as you see, we have to decode 251 twice (in step 1.1 and step 2). This is very inefficient as we do everything more than ones.

To improve this, you can keep track of what you have done so far in a lookup table

kvantour
  • 25,269
  • 4
  • 47
  • 72
  • I believe the OP only needs to find the number of possible ways to decode the string, not to print every possible decoded string. In that case you can write a linear-time dynamic programming solution instead of brute-forcing every possible decoding. – eesiraed Jan 06 '20 at 23:27
  • @bessiethecow you are correct. But since this is a homework problem I don't believe that dynamic programming is useful for him, unless he already has seen that. In the above, the solution is easily adaptable to get the count. Furthermore, I also mentioned that the presented pseudocode is quick and dirty, not necessarily elegant and efficient. – kvantour Jan 07 '20 at 00:06
-1

Check out the ASCII table http://www.asciitable.com/ . I have had something like this similar for my homework as well. since 'a' = 97 and 'z' = 122, you could subtract the desired character from 96 to get the preferred value from the casted character.

For example:

int letterNum {(int)'a' - 97 + 1}; // 1

int letterNum {(int)'z' - 97 + 1}; // 26
Dan T93
  • 1
  • 1
  • If you're going to assume ASCII is used which isn't guaranteed, you can subtract `'a'`, instead of writing the encoding value. `c - 'a' + 1` is much more readable than `c - 97 + 1` and works for other encodings as long as the letters are consecutive. – eesiraed Jan 06 '20 at 23:31
  • I'd assume this is an intro class homework assignment to C++/OOP. And either way, it wasn't a solution, it was just an idea to help solve the problem. – Dan T93 Jan 07 '20 at 00:35
  • Also, I made a couple mistakes on my interpretation. My bad – Dan T93 Jan 07 '20 at 00:38