0

Can't figure out what I am doing wrong here. I am guessing it has to do with the way I am incrementing through the keyword.

Sample below:

firecore@ide50:~/workspace/pset2 $ ./vigenere abcdefghijk <--run program w/ keyword

aaaaaaaaaaa <-----enter text to be incrypted

abcdtabatak <--- result. I am assuming this should encrypt to abcdefghijk*

check50 results:

firecore@ide50:~/workspace/pset2 $ check50 2015.fall.pset2.vigenere vigenere.c

:) vigenere.c exists

:) vigenere.c compiles

:) encrypts "a" as "a" using "a" as keyword

:) encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaSGop\n"

:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CASGOP\n"

:) handles lack of argv[1]

:) handles argc > 2 :) rejects "Hax0r2" as keyword

    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    int main(int argc,string argv[])
    {
string sKeyWord = "";
int iKeyLength = strlen(sKeyWord);           
int iKey[iKeyLength];                        
string sPlainText = "";
int iAccept = 0;

        if(argc != 2)
        {
            printf("Invalid argument! Please enter program name and keyword.\n");
            return 1;
        }
        else if(argv[1])
        {
        iKeyLength = strlen(argv[1]);
        for(int i = 0; i < iKeyLength; i++)
            {
                if (!isalpha(argv[1][i]))
                {
                    printf("Invalid entry, please use letters only.\n");
                    return 1;
                }
                else
                {
                    iAccept = 1;
                    sKeyWord = argv[1];
                }
            }
        }    


    for(int i = 0; i < iKeyLength; i++)           
    {
        iKey[i] = toupper(sKeyWord[i]) - 65;     
    }

      sPlainText = GetString();                    
      int iPlainText = strlen(sPlainText);        

      for(int j = 0, k = 0; j < iPlainText; j++)
      {
        if(!isalpha(sPlainText[j]))
            {
                printf("%c",sPlainText[j]);
            }
        if(islower(sPlainText[j]))
            {
                printf("%c",((((sPlainText[j] - 97) + iKey[k%iKeyLength])%26)+ 97));
                k++;
            }
        if(isupper(sPlainText[j]))
            {
                printf("%c",((((sPlainText[j] - 65) + iKey[k%iKeyLength])%26)+ 65));
                k++;
            }
      }


printf("\n");

return 0;

}

0

The problem lies here: string sPlainText = ""; This declares the variable sPlainText, and more importantly, allocates the space for it - which happens to be length 1 for the end of string marker. Later, you assign a string to it and that string is being written to memory, overwriting whatever happens to follow. If those memory locations are later overwritten by their rightful owner variables, the string gets corruptes, maybe everything is corrupted now. I'm not sure exactly what specific interactions are occuring, but that's exactly what's happening here.

If instead, you wait and declare sPlainText when you call GetString(), the problem is solved. You should look at your other string vars for similar issues though. Same problems, but they're not affecting the outcome. Remember, once a string is declared (i.e., a char array), you cannot change its length.

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

3
  • Thanks for the reply Cliff. I commented out my first line where declared and initialized, string sPlainText = "";. I then changed the getstring() to ,string sPlainText = GetString();. This let the code pass check50 but this still happens (firecore@ide50:~/workspace/pset2 $ ./vigenere abcdefghijk <--run program w/ keyword) (aaaaaaaaaaa <-----enter text to be incrypted) (abcdtabatak <--- result.) I am assuming this should encrypt to abcdefghijk Sorry for the format of this but not sure how to format right in these comment blocks :)
    – Firecore
    May 2 '16 at 3:10
  • Well, I dropped a big hint, but I guess it was too subtle. ;-) string sKeyWord = ""; is exactly the same problem.
    – Cliff B
    May 2 '16 at 3:29
  • Yes yes my bad sorry. Your a beast! Caught some unnecessary declarations i had in there while looking into that. Bad placement of others. passes check50 and i get my abcdefghijk as well :D. I know it says not to make thanks comments but thanks and future thanks for answers you provide from now on. YOU DA MAN!
    – Firecore
    May 2 '16 at 3:55

You must log in to answer this question.

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