计算二维字符串中的所有元音
Count all vowels in 2D string
你好,我试着计算二维字符串中的所有元音。我的程序一直给我错误的输出,我不知道哪里出了问题。
这是我的代码:
int main() {
char strings[3][50] = { "hello WORLD", "hELLO", "Hello" };
printf("%d \n", vowels_count_2D(3, 50, strings));
return 0;
}
int vowels_count_2D(const int rows, const int cols, char string[][cols]) {
for (int i = 0; i < rows; i++) {
int vowels = string[i][0];
for (int j = 0; j < cols; j++) {
if (string[i][j] == 'a' || string[i][j] == 'e' ||
string[i][j] == 'i' || string[i][j] == 'o' ||
string[i][j] == 'u' || string[i][j] == 'A' ||
string[i][j] == 'E' || string[i][j] == 'I' ||
string[i][j] == 'O' || string[i][j] == 'U') {
vowels++;
}
}
return vowels;
}
return 0;
}
输出应为 7
。
我的输出是 107
。为什么?
非常感谢。
循环逻辑不正确:vowels
应该在外循环外初始化为0
,return vowels;
语句移到循环体外。
这是修改后的版本:
#include <stdio.h>
int vowels_count_2D(const int rows, const int cols, const char string[][cols]) {
int vowels = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (string[i][j] == 'a' || string[i][j] == 'e' ||
string[i][j] == 'i' || string[i][j] == 'o' ||
string[i][j] == 'u' || string[i][j] == 'A' ||
string[i][j] == 'E' || string[i][j] == 'I' ||
string[i][j] == 'O' || string[i][j] == 'U') {
vowels++;
}
}
}
return vowels;
}
int main() {
char strings[3][50] = { "hello WORLD", "hELLO", "Hello" };
printf("%d\n", vowels_count_2D(3, 50, strings));
return 0;
}
与其扫描整个二维数组,不如在空终止符或每个字符串处停止:
#include <string.h>
int vowels_count_2D(const int rows, const int cols, char string[][cols]) {
int vowels = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols && string[i][j] != '[=11=]'; j++) {
if (strchr("aeiouAEIOU", string[i][j])) {
vowels++;
}
}
}
return vowels;
}
My output is 107. Why?
您只计算了一行(由于过早 return vowels
)但从 h
的 ASCII 值开始。您应该将 vowels
计数器设置为 0
。
int vowels_count_2D(int rows, int cols,char string[][cols]) {
int vowels = 0;
for (int i=0; i<rows; i++) {
printf("V: %d\n", vowels);
for(int j=0; j<cols; j++) {
if(string[i][j] =='a'|| string[i][j]=='e'||string[i][j]=='i'||string[i]
[j]=='o'||string[i][j]=='u'||string[i][j]=='A'||string[i][j]=='E'||string[i]
[j]=='I'||string[i][j]=='O' ||string[i][j]=='U' )
{
vowels++;
}
}
}
return vowels;
}
你好,我试着计算二维字符串中的所有元音。我的程序一直给我错误的输出,我不知道哪里出了问题。 这是我的代码:
int main() {
char strings[3][50] = { "hello WORLD", "hELLO", "Hello" };
printf("%d \n", vowels_count_2D(3, 50, strings));
return 0;
}
int vowels_count_2D(const int rows, const int cols, char string[][cols]) {
for (int i = 0; i < rows; i++) {
int vowels = string[i][0];
for (int j = 0; j < cols; j++) {
if (string[i][j] == 'a' || string[i][j] == 'e' ||
string[i][j] == 'i' || string[i][j] == 'o' ||
string[i][j] == 'u' || string[i][j] == 'A' ||
string[i][j] == 'E' || string[i][j] == 'I' ||
string[i][j] == 'O' || string[i][j] == 'U') {
vowels++;
}
}
return vowels;
}
return 0;
}
输出应为 7
。
我的输出是 107
。为什么?
非常感谢。
循环逻辑不正确:vowels
应该在外循环外初始化为0
,return vowels;
语句移到循环体外。
这是修改后的版本:
#include <stdio.h>
int vowels_count_2D(const int rows, const int cols, const char string[][cols]) {
int vowels = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (string[i][j] == 'a' || string[i][j] == 'e' ||
string[i][j] == 'i' || string[i][j] == 'o' ||
string[i][j] == 'u' || string[i][j] == 'A' ||
string[i][j] == 'E' || string[i][j] == 'I' ||
string[i][j] == 'O' || string[i][j] == 'U') {
vowels++;
}
}
}
return vowels;
}
int main() {
char strings[3][50] = { "hello WORLD", "hELLO", "Hello" };
printf("%d\n", vowels_count_2D(3, 50, strings));
return 0;
}
与其扫描整个二维数组,不如在空终止符或每个字符串处停止:
#include <string.h>
int vowels_count_2D(const int rows, const int cols, char string[][cols]) {
int vowels = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols && string[i][j] != '[=11=]'; j++) {
if (strchr("aeiouAEIOU", string[i][j])) {
vowels++;
}
}
}
return vowels;
}
My output is 107. Why?
您只计算了一行(由于过早 return vowels
)但从 h
的 ASCII 值开始。您应该将 vowels
计数器设置为 0
。
int vowels_count_2D(int rows, int cols,char string[][cols]) {
int vowels = 0;
for (int i=0; i<rows; i++) {
printf("V: %d\n", vowels);
for(int j=0; j<cols; j++) {
if(string[i][j] =='a'|| string[i][j]=='e'||string[i][j]=='i'||string[i]
[j]=='o'||string[i][j]=='u'||string[i][j]=='A'||string[i][j]=='E'||string[i]
[j]=='I'||string[i][j]=='O' ||string[i][j]=='U' )
{
vowels++;
}
}
}
return vowels;
}