0

So I need to write a program which ciphers text adding x (argv[1] = x) to a text prompted to the user. i.e:

./program 1  //running the program with argv[1] = 1

plaintext: abcd  //prompting the user to write characters he want to cipher
ciphertext: bcde  // returning plaintext ciphered with the argv[1] "key" = 1

This is my code

int main (int argc, string argv[])
{   
    if (argc != 2)
    {
        printf("Usage: ./ceasar key\n");
        return 1;
    }
    else if (argc ==2)
        {
            int k = atoi(argv[1]);
            for (int j = 0, len = strlen(argv[1]); j < len; j++)
            {
                if (!isdigit(argv[1][j]))
                {
                    printf("Usage: ./ceasar key\n");
                    return 1;
                }

            }
            for (int j = 0, len = strlen(argv[1]); j < len; j++)
            {
                 if (isdigit(argv[1][j]))
                {
                    string s = get_string("plaintext: ");
                    printf("ciphertext: ");
                    for (int i = 0, n = strlen(s); i <= n; i++)
                    {

                        if ('@' < s[i] && s[i] < '[')
                        {
                            printf("%c", (s[i] - 'A' + k) % 26 + 'A');
                        }
                        else if('`' < s[i] && s[i] < '{')
                        {
                            printf("%c", (s[i] - 'a' + k) % 26 + 'a');
                        }
                        else
                        {
                            printf("%c", s[i]);
                        }
                        printf("\n");
                        return 0;
                    }  
                }
            }
        }
    }```


The first lines checks if argc !=2, and if argv[1][j] has a non numeric character. Once that is done it will get argv[1] and add it to each character given from the user. but it wont work correctly. 

**Any sugestions?**


Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • 1
    Try your program with `2b` as the key to see if you can determine what the problem is. Perhaps read [`man atoi`](https://linux.die.net/man/3/atoi) for further "hint". – DinoCoderSaurus May 11 '20 at 16:27
  • Does this answer your question? [Got stuck with Caesar.c](https://stackoverflow.com/questions/38187223/got-stuck-with-caesar-c) – user3629249 May 12 '20 at 15:09
  • OT: regarding: `printf("Usage: ./ceasar key\n");` Error messages should be output to `stderr`, not `stdout`. Suggest: `fprintf( stderr, "Usage: %s key\n". argv[0] );` – user3629249 May 12 '20 at 15:12
  • OT: regarding: `for (int j = 0, len = strlen(argv[1]); j < len; j++)` This will cause the compiler to output a warning about comparing a `unsigned` value with a `signed` value. Note: the function: `strlen()` returns a `size_t` (unsigned long int) which is being compared with the `int` value in variable `j` – user3629249 May 12 '20 at 15:15
  • [This may help you](https://www.thecrazyprogrammer.com/2016/11/caesar-cipher-c-c-encryption-decryption.html)to achieve your expected output. input `key = 1` and your `message = "abcd"`. – Ali Hassan Ahmed Khan May 12 '20 at 15:43

1 Answers1

1

I tested your code. It prints one single character which means your final for loop returns too fast. This loop iterates just once because it returns 0 at the end of its iteration. If you move return 0 out of the for loop together with printf("\n"), your code will work.

Solution;

for (int j = 0, len = strlen(argv[1]); j < len; j++)
{
    if (isdigit(argv[1][j]))
    {
        string s = get_string("plaintext: ");
        printf("ciphertext: ");
        for (int i = 0, n = strlen(s); i <= n; i++)
        {

            if ('@' < s[i] && s[i] < '[')
            {
                printf("%c", (s[i] - 'A' + k) % 26 + 'A');
            }
            else if('`' < s[i] && s[i] < '{')
            {
                printf("%c", (s[i] - 'a' + k) % 26 + 'a');
            }
            else
            {
                printf("%c", s[i]);
            }
        }
        printf("\n");
        return 0;
    }
}
Enis Arik
  • 665
  • 10
  • 24
  • Thank you very much! I just did that before reading this and it works! But i came here again because when i check if the program is doing well with check50 i get that is not ciphering as expected. But it does. Its really weird. – Artur Schneider May 12 '20 at 23:22
  • 1
    You welcome. If I answered your question, then please mark my post "as an answer". – Enis Arik Jul 01 '20 at 07:30