0

I am trying to check that the substitution key contains all the alphabets but am getting error: expression result unused for this line of code: s, l ++; Can anyone help, this is my code as of now:

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

    int main(int argc, string argv[])
    {
        int n = strlen(argv[1]);
        int s = 97, l = 65, count = 0;
        if (argc != 2) //check 1 argument given only
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
        else if (n != 26) //check if key contains exactly 26 characters
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
        else //check key contains each alphabet exactly once
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (argv[1][i] == s || argv[1][i] == l)
                    {
                        count ++;
                    }
                }
                s, l ++;
            }
        }
        if (count == 26)
        {
            printf("OKAY!");
        }
    }
1
  • If your intention is to increment s and l then uses++, l++ Commented Apr 29, 2020 at 21:14

2 Answers 2

0

I'm going to suggest you an easier method. You can simply use the ctype.h library. The library provides you with a function called isalpha(). This function literally checks whether a string contains only alphabetical characters. And I do believe instead of s,l++ you better write them separately.

Another thing that I think I found is you could edit your code this way

for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (argv[1][i] == s || argv[1][i] == l)
                {
                    count ++;
                }
                s++;
                l++;
            }
       }
1
  • Thanks for your suggestion but in this case, i am not just checking if all the characters are alphabets but rather whether all 26 alphabets.- a,b,c,d etc are present.
    – Olivia
    Commented Apr 30, 2020 at 10:35
1

If you want to check whether all 26 alphabetical characters are present, you can check whether the characters are not repeated. It works the same way. This was my code. Take a look. Here I checked whether a character is repeated again. :)

string repeated = key; //duplicating another equal string to check if same character repeats itself
            for (int i = 0; i < len; i++) // loop which increases value of i after checking 26 times of k
            {
                for (int k = 0; k < len; k++) 
                {
                    if (i != k) //when i is equal to k it does not check the conditition since that step must be skipped
                    {
                        if (key[i] == repeated[k]) //checking if same character is repeated
                        {
                            printf("Key must not contain repeated characters\n");
                            return 1;
                        }
                    }
                }
            }
0

You must log in to answer this question.

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