Warm tip: This article is reproduced from serverfault.com, please click

C lang compare a 2d array of strings, based on the number of consonants in each string

发布于 2020-11-28 07:21:35

So I have this program, I have to compare the rows based on the number of consonants,i made an array char voc[12] where I've put all the vocals, then I tried to make a function that calculates the total number of vocals and consonants, but it as well doesn't work.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
    
int main()
{
    srand(time(NULL));
    char rand_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char voc[12] = {'a','e','i','o','u','A','E','I','O','U','Y','y','\0'};
    int m,n,i,j,elem=0,vocale=0;
    printf("Introduceti numarul de linii si coloane:");
    scanf("%d %d", &m, &n);
    char a[m][n];
    for (i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            a[i][j]=rand_string[rand() % (sizeof(rand_string) - 1)];
        }
    }
    printf("Matrita e:\n");
    for (i=0; i<m; i++)
    {
        printf(" \n ");
        for(j=0; j<n; j++)
        {
            printf(" %c\t ", a[i][j]);
        }
    }

    for ( i = 0; a[i][j] != '\0'; i++)
    {
        for (j = 0; a[i][j] != '\0'; j++)
        {
            if(a[i][j] == voc[i])
            {
                vocale++;
            }
            else
            {
                elem++;
            }
        }
        printf("\n %d ", vocale);
        printf("\n %d ", elem);
    }
    return 0;
}
Questioner
Vlad
Viewed
0
Vlad 2020-11-30 20:49:05

Well, I've done it. Firstly I wrote a bool function, that determines if a character is a vowel, and if it's not, it counts as a consonant, then I create a random matrix made of random characters, after that, the program calculates the number of consonants for each row then it compares them and it's typing, which row has the most amount of consonants.

Thanks everybody for your help.

#include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <stdbool.h>

bool is_vowel(char c)
{
    char voc[13] = {'a','e','i','o','u','A','E','I','O','U','Y','y','\0'};
    for (int i=0; i<13; i++)
    {
        if(voc[i]==c)
        {
            return true;
        }
    }
    return false;
}


int main()
{
    srand(time(NULL));
    char rand_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int m,n,i,j,elem=0,cons[10],vocale=0;
    printf("Rows and columns:");
    scanf("%d %d", &m, &n);
    char a[m][n];
    for (i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            a[i][j]=rand_string[rand() % (sizeof(rand_string) - 1)];
        }
    }
    printf("The matrix is:\n");
    for (i=0; i<m; i++)
    {
        printf(" \n ");
        for(j=0; j<n; j++)
        {
            printf(" %c\t ", a[i][j]);
        }
    }
    for (i = 0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            if(is_vowel(a[i][j]))
            {
                vocale++;
            }
            else
            {
                elem++;
            }
        }
    }
    printf("\nSum of consonants:%d", elem);
    for (i = 0; i<m; i++)
    {
       cons[i]=0;
       for(j=0;j<n;j++)
       {
           if(!(is_vowel(a[i][j])))
           {
               cons[i]++;
           }
       }
       printf("\n Row %d has %d consonants", i, cons[i]);
                      if(cons[i]>cons[i-1] && cons[i]!=0)
               {
                   printf("\nThe row with the biggest num of consonants:%d", i);
               }
    }


    return 0;
}