0

This is my code for the cipher itself. No mater what key I use it shifts all the letters by only 1, always.

Please HELP!

for (i = 0; i < m; i++) //Loop through user's input's length.
      {
          for (j = 0; j < 26; j++)
              {
                 key[j] = j; //Assign numbers 0 to 25 to each letter of the key.
              }

              if (isalpha(userMessage[i])) //If string index has letter...
              {
                  if (islower(userMessage[i])) //...and if letter is lowercase...
                  {
                      /*...shift characters by key amount inside lowercase ASCII table...*/
                      userMessage[i] = (((userMessage[i] - 97) + key[j % n]) % 26) + 97; 
                  }

                  else if (isupper(userMessage[i])) //...or if letter is uppercase...
                  { 
                      /*...shift characters by key amount inside uppercase ASCII table */
                      userMessage[i] = (((userMessage[i] - 65) + key[j % n]) % 26) + 65;
                  }
              }

              else if (isdigit(userMessage[i]) || isspace(userMessage[i])) //And if string index has non-alphabetical value... 
              {
                  userMessage[i] = userMessage[i]; //...keep value unchanged.
              }

              printf("%c", userMessage[i]); //Print ciphered text.
}
0

The code has several problems. First and most important, it appears to be completely ignoring the key that is supposed to be entered as a parameter when the program is executed at the command line. Look at the following:

      for (j = 0; j < 26; j++)
          {
             key[j] = j; //Assign numbers 0 to 25 to each letter of the key.
          }

All this does is assign numbers 0 to 26 sequentially to the key array. It does nothing with the key that is supposed to be used.

Next, what happens if the plain text contains a number or punctuation?

There may or may not be more issues, but that will depend on the fixes implemented for these problems.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .