0

Kindly help I am not getting the required output. My code doesn't succesfully handles lack of argv[1]. Please point out the mistake.

int main(int argc, string argv[])
{
    string key = argv[1];
    int found=0;
    for (int i=0, n=strlen(key); i<n; i++)
    {
        if (isalpha(key[i]))
        {
            continue;
        }
        else
        {
            found = 1;
            break;
        }
    }

    if (argc==2 && found == 0)
    {
         //code here 
    }

    else
    {
        printf ("Error: Try again!\n");
        return 1;
    }
}

2 Answers 2

1

The main problem is that you are using the same index, i, for the plaintext and the key. The plaintext index will increment for every character in the plaintext string, but the key index should only increment when a letter is encoded, not when a space, punctuation, number, or any non-alpha is processed. the plaintext and key arrays require independent index variables.

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

9
  • Okay I understand I have to use two separate indices but can you please tell should I use nested for loops or two separate for loops? I have tried using two separate indices in the same for loop but the output is not correct.
    – GreatLeo19
    Commented Jul 20, 2016 at 15:26
  • A loop cannot be used to increment the key index because consumption of the key may or may not happen on any pass. You have to use a simple variable to keep count and increment it only when a letter is encoded.
    – Cliff B
    Commented Jul 20, 2016 at 16:36
  • I have used variable but there still seems to be a problem. I declared a variable and initialized it to 0. And then for every alphabet in plaintext I did this
    – GreatLeo19
    Commented Jul 20, 2016 at 19:34
  • Did what? Please edit the question and change to the new code.
    – Cliff B
    Commented Jul 20, 2016 at 19:40
  • I have used a variable but there still seems to be a problem. I declared a variable and initialized it to 0. And then for every alphabet in plaintext I incremented the variable by 1 i.e I replaced every key[i] in the code I posted with key[r] followed by r++. (with r being just a variable I declared to keep track of key). But the output is still incorrect. Kindly help!
    – GreatLeo19
    Commented Jul 20, 2016 at 19:45
0

if you impose an additional condition IF

if(isalpha(p[i])) 
{
 .....
}

......

 else 
{
    printf ("%c", p[i]);
}

FOR loop will not consider whitespace, but if printed, anyway your formula is not correct, note that the formula for Vigenere is exactly the same as for Caesar, but now the key is variable, and should change slightly if key is upper, or is lower, etc but this part have done well.

You must log in to answer this question.

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