0

I have an issue with checking for non alpha symbols

if(check n for alpha)
 {
    if check for lower symbols

        //statement

    else check for upper symbol
        //statement
   print (a)
 }
   else (n is non alpha)
      should print (n)

a is my encrypted text and n is the text that user write.
I get output like this

jharvard@appliance (~/Dropbox/pset2): ./vigenere baz
worldsayhello
xoqmdrbygflkp
jharvard@appliance (~/Dropbox/pset2): ./vigenere baz
world say
xoqmd tas
3
  • Can you please be more clear about what you are asking? Are you getting compiler errors? Is the isalpha function not working properly? What are you seeing that leads you to believe this? The fuction to check length of a word is strlen() – lethaljd Sep 4 '14 at 17:02
  • possible duplicate of How to Wrap Around Array Indexes? – lethaljd Sep 4 '14 at 17:09
  • In short, you need to use the modulo operator to "wrap around". There are several posts about this on here already. Here is one of them: cs50.stackexchange.com/questions/1298/… – lethaljd Sep 4 '14 at 17:10
1

Per the manual page for isalpha()

isalpha()
checks for an alphabetic character; in the standard "C" locale, it is equivalent to (isupper(c) || islower(c)).

In other words, isalpha() basically returns true if the passed in char is an alphabetical character. Otherwise it returns false. Meanwhile,

if (isalpha(c))
{
    // c is alphabetical
    if (isupper(c))
    {
        // c is uppercase
    }
    else
    {
        // c is lowercase
    }
}
else
{
    // c is non-alphabetical
}

Also, how can I check again and again the length of my word and the text? If the text is longer than the word, I get an error.

Calculating the index of the current char in the keyword using the remainder operator is one way of achieving that. Given the expression a % b where a and b are integers, mathematically, the result would be >= 0 && < b.

Since we're supposed to increase the index of the current char in the keyword by one as we encipher the current plain character in the plain text, if the length of the number of plain characters in the plain text is greater than the length of the keyword, eventually, the index of the current char in the keyword will become equal to (and exceed, if we continue) the length of the keyword and we'd wind up with invalid index(es).

Taking the remainder of dividing the index of the current char in the keyword by the length of the keyword will wrap the index back to 0 once it's equal to the length of the keyword.


Edit #0: after looking at the code, I could find the following problems

  1. In general, it's not a good practice to have something like

    if (condition)
    {
        // do nothing
    }
    else
    {
        // do something
    }
    

    you could directly have something like that instead

    if (!condition)
    {
        // do something
    }
    
  2. The cases that should be covered are

    a. if the current character in the plain text is an uppercase char

    • if the current character in the keyword is an uppercase char

    • if the current character in the keyword is a lowercase char

    b. if the current character in the plain text is a lowercase char

    • if the current character in the keyword is an uppercase char

    • if the current character in the keyword is a lowercase char

    what you're covering though is

    a. if the current character in the plain text is an uppercase char

    • if the current character in the keyword is an uppercase char

    b. if the current character in the plain text is a lowercase char

    • if the current character in the keyword is a lowercase char

    c. if the current character in the plain text is a lowercase char OR (the current character in the keyword is an uppercase char OR a lowercase char)

    Now, you claim that your cases work well, but they're really difficult to read and understand.

  3. You shouldn't calculate the index of the current char in the keyword in terms of i because it always increases whether the current char in the plain text is an alphabetical char or not. Rather, you should calculate it in terms of j.

10
  • Okay, I got as a result this: keyword = bacon, text = meet answer = neg? . Why my code still use all characters on output when i write from the beginning isalpha(n) and in the end i write else print text[n]. From where i got this error? – Teodor Ilkov Nikolov Sep 5 '14 at 19:32
  • @TeodorIlkovNikolov looks like there's a logical error with the loops. How do your loops look like? – Kareem Sep 5 '14 at 21:23
  • @TeodorIlkovNikolov I'm sorry, I don't understand any of that. Please rephrase your comment and edit your question if you wanna add something! – Kareem Sep 6 '14 at 14:40
  • Can you check the question again i edit some changes. Thanks – Teodor Ilkov Nikolov Sep 6 '14 at 14:56
  • @TeodorIlkovNikolov it looks like you're calculating the index of the current character in the keyword in terms of the index of the current character in the plain text. Make sure you're not increasing the index of the current character in the keyword in case the current character in the plain text is a non-alphabetical character. – Kareem Sep 6 '14 at 19:09

You must log in to answer this question.

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