0

So when i run the following code in the caesar.c that is located in the caesar folder, it seems to run only for the very first character of the given plaintext and then the next characters are missing. I have tried to run the same code in a file outside of the pset2 directory and it works perfect. Why is this happening and what i can change in the code?

        }
         //get input from the user
         string plain = get_string("Plaintext: ");
         
         printf("ciphertext: ");
         for(int i = 0, z = strlen(plain); i < z; i++)
         {
             // if alphabetical and lowercase  print the corresponding cipher
             if(isalpha(plain[i] && islower(plain[i])))
             {
                  //string cipher = ((((plain[i] - 97) + atoi(argv[1])) %26) + 97);
                 printf("%c", (((plain[i] - 97) + atoi(argv[1])) %26) + 97);
                 //printf("%s", cipher);
                 return 0;
             }
            // if alphabetical and uppercase print the corresponding cipher
            else if(isalpha(plain[i]) && isupper(plain[i]))
            {
                //string cipher = ((((plain[i] - 65) + atoi(argv[1])) %26) + 97);
                printf("%c", (((plain[i] - 65) + atoi(argv[1])) %26) + 65);
                //printf("%s", cipher);
                return 0;
            }
           //check any other non alphabetical character and print it out as it is
           else
           {
               printf("%c", plain[i]);
           }

    }
}
}

this is an example of the output i get:

~/pset2/caesar/ $ ./caesar 3
Plaintext: I'm going back.
ciphertext: L~/pset2/caesar/ $ 

1 Answer 1

0

You are trying to convert the whole string and it is just printing the first letter. Consider using a "for" loop to iterate each char in the string and printing the chars one by one until its done.

You must log in to answer this question.

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