0

I do not get it with the correct conversion string-int in the exercises Initials and Caesar. It tried it with all different solutions and functions, but nothing worked;( Could someone help me please?

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

int main(int argc, string argv[])
{
    if (argc != 2)
        {return 1;}
    else
        {string key = argv[1];
        printf ("plaintext:");
        string text1 = get_string();
        string text2[20];
        for (int i = 0; i < strlen(text1); i++)
            {if (isalpha(text1[i]))
                {if (isupper(text1[i]))
                    **{text2[i] = (text1[i] - 'A' + key) % 26 + 'A';}**
                else
                    {text2[i] = (text1[i] - 'a' + key) % 26 + 'a';}
                }
            else {text2[i] = text1[i];}
        }
    }
}
0

A single char can be treated as an int. It can be added, subtracted, etc. A string (multiple chars) cannot. Look at the following line:

        {text2[i] = (text1[i] - 'A' + key) % 26 + 'A';}

The code tries to perform operations on the array key, in other words, on the entire string. The code needs to select a single char from the key, i.e., key[x].

There may be other issues, but this answers the question.

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

You must log in to answer this question.

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