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

其他-C lang根据每个字符串中辅音的数量比较字符串的二维数组

(其他 - C lang compare a 2d array of strings, based on the number of consonants in each string)

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

所以我有了这个程序,我必须根据辅音的数量比较行,我在数组char voc[12] 中放置了所有的人声,然后尝试创建一个函数来计算人声和辅音的总数,但是它也不起作用。

#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

好吧,我已经做到了。首先,我编写了一个布尔函数,确定一个字符是否为元音,如果不是,则将其视为辅音,然后创建一个由随机字符组成的随机矩阵,此后,程序将计算每个字符的辅音数量然后比较它们并输入内容,哪一行的辅音数量最多。

谢谢大家的帮助。

#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;
}