Search code examples
carraysstringdynamic-memory-allocation

find which line has the most vowels from a file


create function int isVowel( char c ) create function int vowels( char s[] )

Which string from the file contains the most vowels - in the case of the tie, show just the 1st @ obviously should use the functions above is what i am trying to do but i am not entirely sure on how to actually do it i could use some hints

I'm just not entirely sure how to do this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isvowel(char c)
{
    int vowel=0;
    int i;
    int x=0;
    //for(i=0; i<5; i++){
    //  counter[i]=0;}
    for(i=0; i <c; i++)
    {
        if (c=='a' || c=='A')
            vowel++;
        else if (c=='e' || c=='E')
            vowel++;
        else if (c=='i' || c=='I')
            vowel++;
        else if (c=='o' || c=='O')
            vowel++;
        else if (c=='u' || c=='U')
            vowel++;
    }

    return vowel;
}
int main( int argc, char *argv[] )
{
    char max[1024];
    char** buff;
    int i=0 ,num = 5;
    FILE *infile;
    int lc = 0;     //counts how many lines there are
    int p;

    //asks for file name if none is supplied
    if ( argc < 2 )
    {
        printf("Must supply file name\n");
        return 1;
    }
    infile = fopen( argv[1], "r" );
    //checks to see if file opens
    if ( infile == NULL )
    {
        printf("could not open  %s\n", argv[1]);
        return 1;
    }
    buff = (char**)calloc(sizeof(max),sizeof(char));
    int k;
    for(k=0; k<num;k++){
        buff[k] = (char*)calloc(sizeof(max),sizeof(char));
    }
    while(fgets(max,sizeof(max),infile)){
        strcpy(buff[i],max);
        i++;
    }
    int x;
    for(x =1; x<i;x++){
        lc++;
        printf("%d:%s",x,buff[x]);
    }
    p = isvowel(i);
    printf("your number of vowels is:%d",p);

    return 0;
}

should print a string with most vowels right now I can't even get it to count any vowels


Solution

  • A character is either vowel or not:

    int isVowel(char c) {
        switch (c) {
            case 'a':
            case 'A':
            case 'e':
            case 'E':
            case 'i':
            case 'I':
            case 'o':
            case 'O':
            case 'u':
            case 'U':
                return 1;
        }
        return 0;
    }
    

    Now, let's implement your other function:

    int vowels(char s[]) {
        int size = strlen(s);
        int output = 0;
        for (int i = 0; i < size; i++) output += isVowel(s[i]);
        return output;
    }