October 22, 2024
Chicago 12, Melborne City, USA
C#

Find vowels in a string


Given below is my code for finding vowels in a given string:

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

int main() {
    char str[100];
    int i, str_length;

    //scanf("%[^\n]%*c", &str[100]);
    fgets(str, sizeof(str), stdin);
    str_length = strlen(str);
    for (i = 0; i < str_length; i++) {
        if (str[i] == 'a' || str[i] == 'e' ||
            str[i] == 'i' || str[i] == 'o' || 
            str[i] == 'u' || str[i] == 'A' ||
            str[i] == 'E' || str[i] == 'I' ||
            str[i] == 'O' || str[i] == 'U')
        {
            printf("Vowels in the string are: %c \n", str[i]);
        }
    }
    return 0;
}

I just wanna know, why scanf("%[^\n]%*c",&str[100]) is not working in my code?
cause I tried executing my program multiple times using scanf but it didn’t worked.

And after using fgets I got the desired output.

So, can anyone help me to understand, where I am wrong!!



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video