-1

I want to pass two variables into a function in C - one being a string and the other being an individual character from a string.

However, I am not really sure how to use the function without either getting the "Expected Expression" error:

int spaces_away(string cipher[], string plain[char i]);

or getting an "undeclared identifier" error as well:

int spaces_away(string cipher[], char plain[i]);

I think it is also important to add that the variable "i" is from a "for" loop in the code and this is what my code looks like inside of main as of now:

{
    // Error if there are not two arguments
    if (argc != 2)
    {
        printf("Command Line Must Have Two Arguments\n");
        return 1;
    }
    // Error if there are not 26 characters in encryption
    else if (strlen(argv[1]) != 26)
    {
        printf("Cipher must have 26 characters\n");
        return 1;
    }
    else
    // Takes a word and encrypts it by a user given alphabet
    {
        string stdWord = get_string("plaintext: ");
        for (int i = 0, n = strlen(stdWord); i < n; i++)
        {
                encrypt[i] = (stdWord[i] + spaces_away(argv[1], tolower(stdWord[i]));
        }
            printf("ciphertext: %s\n", (string) encrypt);
        return 0;
    }
}


user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    This is tagged as `c`. C does not have a builtin `string` type, have you defined it yourself? If so, that would be needed for an answer. – Marco Dec 20 '21 at 17:18
  • 1
    @marco-a This looks very much like a `cs50` problem. `string` is `char *` and the library code has a `get_string` function – Craig Estey Dec 20 '21 at 17:40

1 Answers1

1

I think it is also important to add that the variable "i" is from a "for" loop in the code

The compiler knows nothing about your variable i in a for loop when it parses the function declaration.:)

If the second parameter has to have the type char then declare it such a way. For example

int spaces_away(string cipher, char c );

or that is the same

int spaces_away( char *cipher, char c );

or

int spaces_away( char cipher[], char c );

Pay attention to that if you are using the alias string then the first parameter must be declared like

string cipher

instead of

string cipher[]

The function tolower should be called like

tolower(( unsigned char )stdWord[i])
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Pardon my silly question, but why `tolower` needs that cast? – kirjosieppo Dec 20 '21 at 18:14
  • @kirjosieppo If the string will contain a character with for example a negative value then such a call of tolower with a negative value can invoke undefined behavior. – Vlad from Moscow Dec 20 '21 at 18:18
  • Character of negative value? That sounds wild – kirjosieppo Dec 20 '21 at 18:21
  • 1
    @kirjosieppo [Do I need to cast to unsigned char before calling toupper(), tolower(), et al.?](https://stackoverflow.com/q/21805674/2505965) (`c++` tagged, but still relevant to C in this case) – Oka Dec 20 '21 at 18:21
  • @kirjosieppo There are extended ASCII tables for different languages. Also you can directly assign a negative value to any element of a character array. – Vlad from Moscow Dec 20 '21 at 18:22
  • @VladfromMoscow Yes, of course. So the cast just a good general practice. – kirjosieppo Dec 20 '21 at 18:25