0

currently i am solving pset2 vigenere cipher problem.bellow is my code.problem is that when i execute this code(this code is only for uppercase of key and plain text)the output is fine.but when i want to check that key was also in uppercase it will give output only the length of key using in argv[1].(when i replace this line"if (isupper(text[a]))" with this one "if ((isupper(text[a])) && (isupper(argv[1][b])) please help me.

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

int main(int argc,string argv[])
{
    if (argc !=2)
    {
        printf("command line argument error\n");
        return 1;
    }

    for ( int i = 0; i < strlen(argv[1]); i++)
    {
        if (!isalpha(argv[1][i]))
        {
            printf("command line argument error\n");
            return 1;
        }
    }

    printf("plaintext:");
    string text = get_string();
    printf("ciphertext:");
    for (int a = 0, b = 0,n = strlen(text); a < n; a++)
    {
        char key = argv[1][(b) % strlen(argv[1])];
        if (isalpha(text[a]))
        {
            if (isupper(text[a]))
            {
                char letter = text[a] - 65;
                char cipher = (letter + (key-65)%26)+65;
                b++;
                printf("%c", cipher);
            }

        }

    }

} 
3
  • When posting code, in the editor, select the code block, and click the {} button or press Ctrl+K to indent that code by four spaces. A block of lines all starting with at least four spaces is considered code (those four spaces are removed on rendering). Outside code blocks, the # is considered markdown for a caption, that's why your #include statements all look weird.
    – Blauelf
    Nov 6 '17 at 13:03
  • Also, there's inline code. Code wrapped in backticks (`) is also protected from being interpreted as markdown (really useful with code containing multiple *).
    – Blauelf
    Nov 6 '17 at 13:07
  • Thanks Blauelf .actually this is my first post and i cant figure out how to post the code in right way.next time i remember your advice. Nov 7 '17 at 17:01
0

The only problem I can foresee in using

if(isupper(argv[1][b]))

is that it will be error-prone as b will eventually go beyond the length of your keyword. Since you're already storing each character of the key using

char key = argv[1][(b) % strlen(argv[1])];

Just try this?

if(isupper(key)) {
    // your code here
}
1
  • thanks dear .your help solve my problem. Nov 6 '17 at 6:40

You must log in to answer this question.

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