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

int main(int argc, string argv[])
{
    if (argc != 2)
        return 1;
    else
    {
        string s = GetString();
        printf("\n");
        int k = atoi(argv[1]);
        for (int i = 0, n = strlen(s); i < n; i++)
        {
            if (isalpha(s[i]))
            {
                if (isupper(s[i]))
                {
                    int a = 's[i]' - 65;
                    int asciii = (a + k) % 26;
                    int asciiii = asciii + 65;
                    printf("%c", asciiii);
                }
                else 
                {
                    int a = 's[i]' - 97;
                    int asciii = (a + k) % 26;
                    int asciiii = asciii + 97;
                    printf("%c", asciiii);
                }
            }
            else
            {
                printf("%c", s[i]);
            }
        }
        printf("\n");
    }
}

for example, if I input ./caesar 5 and specify the string to be for example "fds", the output will be as follow: vvv

I ve already spent about 2 and half hours with this and still didnt manage to come up with a solution.

0

It's a syntax error. Look at this line:

int a = 's[i]' - 97;

You are subtracting 97 from the literal text 's[i]', not the value stored in the var s[i], so you are always producing the same result. ('s[i]' seems to be evaluating as the right bracket ] .) You need to remove the single quotes around s[i]. They are causing the content between the single quotes to be treated as text instead of as a variable.

Interestingly, it actually compiles. Normally, a string (multiple characters) will not be accepted where a char should appear.

If this answers your question, please click on the check mark. Let's keep up on forum maintenance. ;-)

4
  • Thank you. It does work. But I have a question. If I do int a = "A", it wont work. I have to type it as int a = 'A' for it to work and save the ascii number into the variable. So why does x[i] works when basiclly its just a way to say "ith letter"? – Martin Wzatek Mar 17 '16 at 20:27
  • Double quotes say that something is a string. Single quotes say it is a single char. a char array is a string, so x is a string, but x[i] stores a single char. – Cliff B Mar 17 '16 at 20:32
  • As a side note, in your code, you create a number of new variables as you work through the encoding calculation. While technically valid, it isn't necessary. Unless you have a need to save the contents of a variable before the final calculation, you can reuse it to do the work. For example, you could do this: int a = 's[i]' - 65; a = (a + k) % 26; a = a + 65; printf("%c", a); } After that, you could simplify it to int a = ( ( ('s[i]' - 65) + k) % 26 ) + 65 ; – Cliff B Mar 17 '16 at 20:37
  • Now I get it, thanks! – Martin Wzatek Mar 17 '16 at 20:38

You must log in to answer this question.

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