0

I spent about a week on this problem and just cannot figure out how to solve this. I am still needed to 1. reject an alphanumberic key and 2. do not increment if the character is anything other is isalpha. Can someone please point me in the right direction on how to solve these last two questions? is there no other way to reject an alphanumeric key other than doing a for loop on each character? thank you

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



int main(int argc, string argv[])

{
     if (argc == 2)
        {
            string text = get_string();




// changes key to integer//

int cipher;





for (int i=0; i<strlen(text); i++) 
    {
    int chr = text[i];
    int x = i % strlen(argv[1]);   
        if (isalpha(chr) && isupper(chr))

                        {
                            cipher = ((chr-65+argv[1][x]+65) % 26)+65;
                        }
        else if (isalpha(chr) && isupper(chr))

                        {
                            cipher = ((chr-65+argv[1][x]+65) % 26)+65;
                        }    

        else if (isalpha(chr) && islower(chr)&& islower(argv[1][x]))
                        {
                            cipher = ((chr-97+argv[1][x]-97) % 26)+97;
                        }
        else if (isalpha(chr) && islower(chr)&& isupper(argv[1][x]))
                        {
                            cipher = ((chr-97+argv[1][x]-65) % 26)+97;
                        }               



        else{       
                    cipher = chr;

                        }


         printf("%c", cipher);
    }

    }



        // how to reject when user enters a number?//
            else{
        printf("ENTER A WORD!!\n");
            return 1;
            }

printf("\n");    
return 0;  
}
1

Iterating through each character is correct in C. Try looking into 'isdigit()' for checking for numbers.

For the increment issue, I used a counter variable which only increments if the current input 'isalpha'. EG:

int counter = 0;
if (isalpha(chr))
{
    // do stuff

    counter++;
}

Then use the counter variable to calculate the current key index ('x' in your case) instead of using 'i' from the loop:

x = counter % strlen(argv[1])

It may help to separate the isalpha check from the isupper/islower checks to achieve this. It would also mean it only needs to check isalpha once regardless of upper/lower case.

Additional: Don't forget you can exit the program before any processing is done in a self contained error check. EG:

if (argc !=2)
{
    printf("Something went wrong")
    return 1;
}

This will exit the code at this point without continuing through any other code and is easier to see than encasing your whole code in an if statement. It also allows for different outputs from different error.

Hope this helps. Feel free to ask if anything is unclear and don't forget to mark as the answer if you find this helpful :)

EDIT

Code from comment:

if (!isalpha(chr) && isdigit(chr))
{
    printf("Only use alphabetic characters");
    return 1;
}
2
  • for the keyword check, isdigit won't handle if "!","$","@" are used, how did you account for symbols Oct 28 '16 at 2:22
  • isalpha should handle non-alphabetic characters. So: if (!isalpha(chr) && isdigit(chr)) {return 1}
    – Ian Grover
    Oct 28 '16 at 7:45
0

I understand your counter concept, am i placing it in a bad position because it doe not increment

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



    int main(int argc, string argv[])

{

     if (argc != 2)
        {
             // how to reject when user enters a number?//

        printf("ENTER A WORD!!\n");
            return 1;
        }

        else if (argc == 2)
        {
            string text = get_string();




// changes key to integer//

int cipher;
int counter = 0;
int x = counter % strlen(argv[1]);

for (int i=0; i<strlen(text); i++) 
    {
    int chr = text[i];


        if (isalpha(chr))
          {

                if (isupper(chr))

                        {
                            cipher = ((chr-65+argv[1][x]+65) % 26)+65;

                        } 
                else if (islower(chr)&& islower(argv[1][x]))
                        {
                            cipher = ((chr-97+argv[1][x]-97) % 26)+97;

                        }
                else if (islower(chr)&& isupper(argv[1][x]))
                        {
                            cipher = ((chr-97+argv[1][x]-65) % 26)+97;

                        }



             counter++;  



    }
        else{       
                    cipher = chr;

                        }


         printf("%c", cipher);
    }

    }



printf("\n");    
return 0;  
}
0

Never mind, just figured out the counter variable strat. Now just need to figure out a way to reject a bad keyword. almost there!

You must log in to answer this question.

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