0

The sample input contains: 3, omahgoToRuLob, OmAhgotUrulobEI, aYfun The expected output is: ugly string pretty string pretty string

I received an error such as: 22:7: error: array size missing in ‘string’ 27:23: error: expected expression before ‘]’ token 29:14: error: expected expression before ‘]’ token

My code is in c programming.

#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main(){
    int i;
    char string[100];
    char vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"};

    for (i = 1; i < 4; ++i)
    {
        scanf("%99s", string[]);

        if (string[] == vowels) 
            printf("lovely string \n");
        else
            printf("ugly string \n");

    }

    return 0;
}
Chewjunnie
  • 137
  • 6
  • Try `char string[];` --> `char string[100];` and `scanf("%s", &string[]);` --> `scanf("%99s", string);`. `string[] == vowels` --> research `strspn()` – chux - Reinstate Monica Mar 29 '21 at 02:35
  • I have edited my code like u suggest but i still received the same error. – Chewjunnie Mar 29 '21 at 02:49
  • The subscript operator `[]` is used to denote that the programmer desires to access the i-th index of an array. So, the compiler expects an index value (e.g. 0, 1, 2, etc.) before `]` token. – Rohan Bari Mar 29 '21 at 02:52
  • @Chewjunnie Looks like you skipped on researching `strspn()`. – chux - Reinstate Monica Mar 29 '21 at 03:01
  • i did researched on strspn(), but how can i use it on my code? It doesnt have a bool, thus i cant used T/F function to print the respective function – Chewjunnie Mar 29 '21 at 03:09
  • 1
    It's hard to help you because almost none of this makes sense. Which is to say, I can't tell you how to fix what you wrote because I can't tell what you expect your code to mean. For example, where you have `if (string[] == vowels)`, what exactly do you expect to be compared to what? You do understand that `[]` is *not part of the variable name*, right? And if you want to know whether a vowel is *in* some string, how does it help you to check whether the string *is equal to* something? – Karl Knechtel Mar 29 '21 at 03:11
  • i want to compare whether the sample input contains a vowel – Chewjunnie Mar 29 '21 at 03:14

1 Answers1

2
  1. You can't declare an array without defining the array size, unless you do it dynamically
  2. You declare one char, but you assign all the vowels to this char.
    • char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
  3. scanf("%s", string); is the right way to use scanf() with strings. Also, you want this out of your for loop, because you just get the input once
  4. You have a variable i in your loop that you don't use. Also, you have i<4 while the input might be larger
  5. Read this about i++ and ++i
  6. Also you have to compare each char of the input string with each char of the vowels array
  7. You don't need to store both lowercase and uppercase vowels. Use the tolower(char c) function from ctype.h to convert the input characters to lowercase and compare them only to lowercase vowels.
Tomás Ferrer
  • 55
  • 1
  • 6
leech
  • 367
  • 1
  • 4
  • 16