c - 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;
}
解决方案
嗯,我已经做到了。首先我写了一个布尔函数,它判断一个字符是否是元音,如果不是,它算作辅音,然后我创建一个由随机字符组成的随机矩阵,然后程序计算每个辅音的数量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;
}
推荐阅读
- java - 映射多对一关系
- angular - ngbDropdown 键盘箭头键导航
- javascript - 反应
组件问题:我无法让它工作 - apache-spark - Spark 结构化流式传输是否适合亚秒级延迟的流式传输作业?
- sql - 春季批处理表大小限制
- java - 从本地和远程连接 RMI
- android - 如何以编程方式在android中启用/禁用设备纵向模式?
- powerbi - Excel文件之间通过power query读取文件不同
- axes - 是否有一种“快速”的方式来获取有关可变 Google 字体的信息?
- git - 检查某个脚本是否修改了任何签入文件的 GitHub 操作