Home > Blockchain >  Count all vowels in 2D string
Count all vowels in 2D string

Time:11-28

Hello so I´ve tried to count all vowels in my 2D string. And my program keeps giving me wrong output and I don´t know what´s wrong. This is my code :

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

Output should be 7. My output is 107. Why?

Thank you very much.

CodePudding user response:

The loop logic is incorrect: vowels should be initialized to 0 outside the outer loop and the return vowels; statement moved outside the loop body.

Here is a modified version:

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

Instead of scanning the full 2D array, you should probably stop at the null terminator or each string:

#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] != '\0'; j  ) {
            if (strchr("aeiouAEIOU", string[i][j])) {
                vowels  ;
            }
        }
    }
    return vowels;
}

CodePudding user response:

My output is 107. Why?

You count only one row (due to premature return vowels) but start from ASCII value of h. You should set that vowels counter to 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;
}
  • Related