0

I am getting issues in output for the text where we have a space or any other character except alphabets.Key characters are not going with correct s[i], for example:

if i have a text meet me at the park and key word Bacon then the character after space in the text gets with b rather than n.I am not able to fix it,

Plz help

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

int main (int argc,string argv[])
{
    if ( argc!=2)
    {
        printf("./vigenere string\n");
        return 1;
    }
    int n=strlen(argv[1]);
    string s=GetString();

    for (int i=0,l=strlen(s);i<l;i++)  
    {  
        if(isalpha(s[i])&& isalpha(argv[1][i%n]))
        {
            if (isupper(argv[1][i%n]) && isupper(s[i]))
            {
                char d=argv[1][i%n];
                int t=(int)d;
                t=t-65;
                int u=(int)s[i];
                u=u-65;
                int v=(t+u)%26+65;
                printf("%c",(char)v); 
            }

            if (islower(argv[1][i%n]) && islower(s[i]))
            {
                char d=argv[1][i%n];
                int t=(int)d;
                t=t-97;
                int u=(int)s[i];
                u=u-97;
                int v=(t+u)%26+97;
                printf("%c",(char)v);

            }

           if( isupper(argv[1][i%n]) &&islower(s[i]))
           {
               char d=argv[1][i%n];
               int t=(int)d;
               t=t-65;
               int u=(int)s[i];
               u=u-97;
               int v=(t+u)%26+97;
               printf("%c",(char)v);
           }

           if( islower(argv[1][i%n]) &&isupper(s[i]))
           {
               char d=argv[1][i%n];
               int t=(int)d;
               t=t-97;
               int u=(int)s[i];
               u=u-65;
               int v=(t+u)%26+65;
               printf("%c",(char)v);
           }
      }    
      else  
      {  
          printf("%c",s[i]);
      }
   }
}

2 Answers 2

1

When there is a space or character other than alphabets, i gets still incremented. Since you are using i for both strings to shift. bacon also gets shifted to next element. I would suggest to use another variable instead of i for bacon.

for example, declare a variable before for Loop. lets say i

 int j = 0; 

and use j in all places in

 argv[1][j%n]

instead of i;

In that case whenever you enter into one of the if's, Increment j.

for example:

 if (isupper(argv[1][j%n]) && isupper(s[i]))
        {
            char d=argv[1][j%n];
            int t=(int)d;
            t=t-65;
            int u=(int)s[i];
            u=u-65;
            int v=(t+u)%26+65;
            printf("%c",(char)v); 

            // Here increment j
            j++;
        }

You have to replace other i wiht j as well.

good luck.

2
  • Thanks a tonne ...it works perfectly fine now. Jun 16, 2016 at 13:26
  • please accept this answer as a solved if you are satisfied. thanks
    – Wafeeq
    Jun 16, 2016 at 13:35
1

first, your for loop is not well written out, for example I see no condition. hopefully it's a code Post problem. fix it first. then your parenthesis are not completely matched, like you didn't close the main and the else.

You must log in to answer this question.

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