0

Hi, I don't know how to solve this. I am having problems with the 'cycle' that should occur when z is exceeded or a negative offset is placed that exceeds a. I think I would have to rethink the whole program. Sorry for my english and I hope you can help me or guide me :)

**Write a C++ program (with only if and else) to encode a letter. The encoding consists of returning the letter of the alphabet that is offset places to the right or to the left of the entered letter. The location to the right is given when the offset value is positive. On the other hand, if the offset value is negative the offset will be to the left. An offset value of 0 is considered invalid.

  • In the right path, after the letter z, the letter a is considered to be coming. The same for the uppercase alphabet, if the entered letter is uppercase.
  • On the left path, if the letter a is exceeded, the letter z is considered as coming. The same for the uppercase alphabet, if the letter entered is uppercase. It is required to validate that the values entered are valid:
  • The value of the offset must be in the range (-26;26) and different from 0.**

1 Answers1

0

let's consider all your letters are not capital which will make things easier.


char* encodeMessage(const char* input, int arraySize, int offset) {
    
    char* encoded = (char*) malloc(arraySize);
    
    for(int i=0;i<sizeof(input) / sizeof(char);i++) {
        if(input[i] >= 'a' && input[i] <= 'z') {
            char newLetter = input[i] - 'a';
            newLetter += offset;
            newLetter = newLetter % 26;
            if(newLetter < 0) {
                newLetter += 26;
            }
            encoded[i] = newLetter + 'a';
        // added this condition to avoid encoding null characters
        } else {
            encoded[i] = input[i];
        }
        
    }
    return encoded;

}

you can easily modify it to encode capital letters.

midugh
  • 608
  • 5
  • 21
  • 2
    `sizeof(input)` evaluates to the size of the pointer, not the number of characters in the input string. – Eric Postpischil May 14 '21 at 20:42
  • 2
    This is not “pure C” in the sense of being strictly conforming, as it relies on properties of the character encoding that are not guaranteed by the standard, notably that the codes for the letters a to z are consecutive and in ascending order. – Eric Postpischil May 14 '21 at 20:43
  • 1
    There should likely be a provision, in allocation and assignment, for a null character at the end of the encoded string. – Eric Postpischil May 14 '21 at 20:44
  • 1
    While standards of C do not guarantee any specific character encoding, pretty much every common encoding places characters `a` to `z` in consecutive and ascending orders. – Alex Reynolds May 14 '21 at 21:32
  • @Alex: EBCDIC does not, and it is not too soon to teach students not to make assumptions. That is a valuable lesson. – Eric Postpischil May 14 '21 at 22:30
  • True, but arguably punchcards are no longer common technology. – Alex Reynolds May 15 '21 at 04:02