首页 > 解决方案 > C lang 根据每个字符串中的辅音数量比较一个二维字符串数组

问题描述

所以我有这个程序,我必须根据辅音的数量来比较行,我做了一个数组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;
}

标签: cstring

解决方案


嗯,我已经做到了。首先我写了一个布尔函数,它判断一个字符是否是元音,如果不是,它算作辅音,然后我创建一个由随机字符组成的随机矩阵,然后程序计算每个辅音的数量row 然后它比较它们并输入,哪一行的辅音最多。

谢谢大家的帮助。

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

推荐阅读