0


I thought I had coded a loop for the plaintext input characters to be converted by the cipher, but my output is only the first character.
I don't know where I have gone wrong, any pointers?

Also, I think I may have coded the keyword key into numbers twice but I can't check as I can't get the whole output. Any help?

Here is my code:

int main(int argc, string argv[])
{  
    //just the program and the keyword
    if (argc !=2)
    {
        printf("You have not entered the encryption key correctly.\n");  
        return 1;
    }         
    else
    {      
        //check each letter in keyword is alphabetic characters
        for(int i = 0; i < strlen(argv[1]); i++)
        {
            if (!isalpha(argv[1][i]))
            {
                printf("Only alphabetic characters can be used.");
                return 1;
            }
        }

        {   //convert argv1 to numbers
            string keyword = argv[1];
            int keylength = strlen(keyword);
            int key[keylength];   

            for(int l = 0; l < keylength; l++)
            { 
                key[l]=toupper(keyword[l]) - 65;
            }

            //get the message to be ciphered       
            { 
                string text = GetString();

                //change each letter for the key 
                for(int j = 0; j < strlen(text); j++)
                {
                    //iterate through the plaintext if alpha character
                    if(isalpha(text[j]))
                    { 
                        int k = 0;

                        //iterte through 
                        for(int k =0; k < keylength; k++)
                            if(islower(text[j]))
                            { 
                                int a = (((text[j] - 97 + key[k]) % 26) + 97); 
                                printf("%c", a);
                            }

                        if(isupper(text[j]))
                        { 
                            int b = (((text[j] - 65 + key[k]) % 26) + 65);
                            printf("%c", b);
                        }
                    }

                    else 
                    {  
                        //if not upper or lower case letter then just print
                        printf("%c", text[j]);
                    }

                    printf("\n");
                    return 0;
                }   
            }  
        }
    }    
} 
1

First of all, you should run your code through style50 to format everything in a more readable format until you are consistently formatting correctly on your own. In this case, it would have exposed some issues with curly brace pairings.

Your first else clause is not necessary. If the first IF condition is true, the program is going to exit immediately. If it isn't true, the remaining code will execute, as desired. There's no need to surround it all with an else clause. This will only introduce opportunity for errors.

Next, you're structure implements a loop within a loop. This won't work. This effectively applies every key letter to every plain text letter. Instead, you should use a simple int variable to keep track of the position in the key, resetting to 0 when appropriate.

There may be other issues, but this will get you going.

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 .