0

I still have a lot work to do. But for some reason if I type

plaintext: b

string s = a

ciphertext: b0

I can't seem to find the error any hints would be appreciated. Please don't give away the answer unless it is really obvious.

I commented the lines to explain how the code should work

int encrypted_txt = (int) (s[counter]-65) +( (int)(key[increase]-65) ) % 26;

encrypted_txt = encrypted_txt + 65 ;

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>

//plaintext becomes encrypted text from command line string                   

int main(int argc, string argv[])
{   
    //print when argc equal to 2 and alphabetic     
    if   (argc != 2))
    {
        printf ("invalid input");
        return 1;    
    }  
    // becomes not command line 
    string key = argv[1];

    //string becomes int 
    printf ("plaintext:  ");  
    string s = get_string();
    // make sure strin/g is not to long
    if (s != NULL)   
    {     
        printf("ciphertext: ");   

        // you end up with  s[counter]  

        for (  int counter = 0, j=strlen(s); counter<j; counter++  )                                                                                                              
        {                   
            for(int increase = 0;   increase <= strlen(key); increase++)
            {
                //  alphabetic characters 
                if (isalpha (s[counter]))
                {                   //uppercase
                    if ( ( isupper (s[counter]) ) || ( isupper (key[increase]) ) )
                    {
                        ////97-97 +(0)    +  98-97=(1)    = 1 % 26 = 1 
                        int encrypted_txt = (int) (s[counter]-65) +( (int)(key[increase]-65) )  % 26;
                        //1+65
                        encrypted_txt =   encrypted_txt    + 65 ;                               
                        printf ("%c" ,(char) encrypted_txt);
                    } 
                    // lowercase
                    else if ( (islower (s[counter]) ) || ( islower (key[increase]) ) )
                    {                                                    
                        int encrypted_txt = (int) ( (s[counter]-97) + (int)(key[increase]-97) ) %26; 
                        encrypted_txt =   encrypted_txt     + 97;

                        printf ("%c" , (char) encrypted_txt);
                    }             
                }  
                // when non alphabetic 

                else 
                {    
                    int encrypted_txt =  (int) s[counter];
                    printf ("%c"  , (char) encrypted_txt);                 
                }  
            }    
        }

    }        

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

To get a hint, answer these two questions (the problem should become obvious):

  • How long is k?
  • How many times will the increase loop execute?

Some unsolicited comments: looping through the key with a for loop is going down the wrong road. You only want to 1) increase the key's index when you use it (which is conditionally) and 2) not let the key's index go "out of bounds".

3
  • Thanks I am not going to give this a check mark just because I am not finished and may run into trouble..
    – Tom55555
    Jun 22 '17 at 3:47
  • 1
    If this answered your question you should mark it as accepted. The answer was not given so that you could finish your pset, but so that you could overcome the problem you described in your question. If you run into further trouble you can always ask a new question.
    – ChrisG
    Jun 22 '17 at 7:27
  • can someone tell me if I am on the correct track? k = strlen(s) % strlen(key) increase = 0; while ( increase < k) { increase++ } why can't i go like this? (int increase = 0, k =strlen(s) % strlen(key); increase < k; increase++)
    – Tom55555
    Jun 24 '17 at 7:49

You must log in to answer this question.

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