0

I'm trying to make a Vigenère encryption code for my problem set of the week and I'm almost done. I have a little problem, I can't make the keyword char shift only on alphabetical chars this is my code and I can't track the problem.

GetString() is implemented by library (it's like scanf) & string typedef also

int main(int argc, string argv[])
{
    string keyWord;      
    if( argc != 2  )
    {
        printf("Wrong Argument");
        return 1;            
    }
    else
    {
        keyWord = argv[1]; 

        //check if argument is 
        //only alphabetical characters
        for(int i = 0; i < strlen(keyWord); i++)     
        {
            char c = keyWord[i];
            if( !isalpha(c) )
            {
                printf("Your Keyword Must Contain Only alphabetical characters\n");
                return 1;
            }                                    
        }
    }

    string plainText = GetString();       

    for(int i = 0,j = 0; i < strlen(plainText); i++,j++) 
    {                        
        if(j >= strlen(keyWord))
            j = 0;                

        char c = plainText[i];
        int keyWordWrapper;
        char keyC;                                  

        if(isalpha(c))
            {                     
                keyWordWrapper = j % strlen(keyWord);
                keyC = keyWord[keyWordWrapper];
                int key;
                tolower(c);

                if(islower(keyC))
                 key = keyC - 'a';

                if(isupper(keyC))
                 key = keyC - 'A';                                         
                c = (c - 'a'  + key) % 26 + 'a'; 
            } 
        printf("%c",c);                      
    }

printf("\n");
return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nick
  • 393
  • 2
  • 4
  • 10
  • `tolower(c);` --> `c=tolower(c);` – BLUEPIXY Apr 04 '14 at 12:40
  • oh thanks i didnt saw that... so how to maintain the case after it is lowered? i mean in the prinf what was upper to remain upper and what was lower to remain lower but it is not my main question, the problem with this code is that its not ignoring space – Nick Apr 04 '14 at 12:46

1 Answers1

0

you may do the following

    //to skip the spaces
    if(c==32)
      {
         j--;
         continue;         
      }

    char c = plainText[i];
    int keyWordWrapper;
    char keyC;       
    if(isalpha(c))
        {                     
            keyWordWrapper = j % strlen(keyWord);
            keyC = keyWord[keyWordWrapper];
            int key;
            tolower(c);

            if(islower(keyC))
            {
               key = keyC - 'a';
               c = (c - 'a'  + key) % 26 + 'a'; 
            }
            if(isupper(keyC))
            {
              key = keyC - 'A';                                         
              c = (c - 'A'  + key) % 26 + 'A'; 
            }
         }
Tauseef
  • 2,035
  • 18
  • 17
  • so continue does what? goes to the next loop ignoring the code that follows after? this is a wrong answer... – Nick Apr 04 '14 at 13:00
  • put `if` statement right after the loop starts, it will not skip the `keyC` and ignore the space, also added the `j--;` in the new `if` block. hope that helps. – Tauseef Apr 04 '14 at 13:07