0

I have written this codefor (int i = 0 , n = strlen(alphabet); i < n ; i++), but only the first character has been verified. I think I need another loop, so I have written this code for (int j = 0 ; j <= i; j++) but it also not working. I want to encrypt the input text, so I want the loop to be verified from all the letter in the words. So this program encrypts just one letter. I think to the problem from the loop for (int i = 0 , n = strlen(alphabet); i < n ; i++)

#include

include

include

include

include

int main(int argc, string argv[]) { if (argc < 0 ){ printf("usage: ./caesar key"); return 1; }

int k = atoi (argv[1]);
 string alphabet = get_string("plaintext: ");
int c =0 , alph = 0 ;
for (int  i = 0 , n = strlen(alphabet); i < n ; i++)
 {
      alph = alphabet[i];
  //   for (int j = 0 ; j <= i; j++){
    if (isalpha (alph)){

        if (isupper (alph)){
            alph = alph -'A';
            c = (alph + k) % 26 ;
            alph = c + 'A' ;
        } else {
          alph = alphabet[i] - 'a' ;
            c = (alph + k) % 26 ;
           alph = c + 'a' ;
        }

    }

    printf("ciphertext: %c \n", alph);
   return 0;
    // }

 } }
1
  • Your code needs more context. Please post all the related code so we can see what's really going on. – Cliff B May 24 '19 at 2:51
0

The reason this loop for (int i = 0 , n = strlen(alphabet); i < n ; i++) only processes the first letter is because of the placement of return 0;. It is inside the loop, and "return"s from the main function, which ends the program. Program should not return until after the loop finishes executing.

You must log in to answer this question.

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