Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

cs50 vigenere.. check50 error/ can't find the bug

0 votes
asked Mar 31, 2018 in Pset2 by chawdavanraj58 (120 points)

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

int i, n, j, m, a ;
char C ;

int main(int argc, string argv[])
{
    //checks the argument value//
    if (argc == 2)
    {
        //takes the key and converts the key from string to integer//
        string key = argv[1];

 

        for(j = 0, m = strlen(key); j < m; j++)
        {
            if(isalpha(key[j]))
            {
               continue;
            }
            else
            {
                return 1;
            }
        }

              // takes plain text from the user//

             string PLAIN_TEXT;
             PLAIN_TEXT = get_string("plaintext- ");

              printf("ciphertext: ");
              j = 0;


             // runs loop till the length of string//

        for(i = 0, n = strlen(PLAIN_TEXT); i < n; i++)
        {
            if(isalpha(PLAIN_TEXT[i]))
            {
            a = j % m;


                //checks uppercase letter//
            if (isupper(PLAIN_TEXT[i]) && isupper(key[a]))
            {
                //formula converts ascii to alphabetic and then agin to askii
                printf("%c", (PLAIN_TEXT[i] - 'A' + key[a] - 'A') % 26 + 'A');

            }
            else if(isupper(PLAIN_TEXT[i]) && islower(key[a]))
            {
                 printf("%c", (PLAIN_TEXT[i] - 'A' + key[a] - 'a') % 26 + 'A');
            }


                // checks lowercase letters//
            if(islower(PLAIN_TEXT[i]) && islower(key[i]))
            {
                printf("%c", (PLAIN_TEXT[i] - 'a' + key[a] - 'a') % 26 + 'a');
            }
            else if(islower(PLAIN_TEXT[i]) && isupper(key[i]))
            {
                printf("%c", (PLAIN_TEXT[i] - 'a' + key[a] - 'A') % 26 + 'a');
            }
            j++;
            }

                //checks spaces//
            if (isspace(PLAIN_TEXT[i]))
            {
                printf(" ");
            }

            if (ispunct(PLAIN_TEXT[i]))
            {
                 printf("%c", PLAIN_TEXT[i]);
            }


        }


        printf("\n");
        return 0;

 

    }
    else
    {
        printf("%s k", argv[0]);
        printf("\n");
        return 1;
    }
}

here are check50 results-

:( encrypts "hello, world!" as "iekmo, vprke!" using "baz" as keyword
    expected "ciphertext: iek...", not "ciphertext: ieku, bvxk
:) handles lack of argv[1]
:) handles argc > 2

Please log in or register to answer this question.

Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

197 questions

248 answers

217 comments

4,952 users

9 Online
0 Member And 9 Guest
...