Wrote a program to separate vowels and consonants from a string. Counter variable is added in an unusual manner [closed]

I have written a program in C where the user gives an input string and the program separates the vowels and consonants into different strings and prints the output but the counter variable does not start from zero and increase in an sequential order.

Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
#include <string.h>
#include <ctype.h>
void append(char string[] , char letter)
{
char arr[] = {letter , ''};
strcat(string,arr);
}
void checkcon(char letter,char string[] , int counter)
{
char consonants[] = "bcdfghjklmnpqrstvwxyz";
for (int i=0;i<=20;i++)
{
if (letter == consonants[i])
{
append(string,letter);
counter = counter + 1;
printf("Con : %dn",counter);
}
}
}
void checkvow(char letter,char string[],int counter)
{
char vowels[] = "aeiou";
for (int i=0;i<=4;i++)
{
if (letter == vowels[i])
{
append(string,letter);
counter = counter + 1;
printf("Vow : %dn",counter);
}
}
}
int main(void)
{
char inp[5000] = {0};
char vow[5000] = {0};
char con[5000] = {0};
int vowno,conno = 0;
int length;
printf("Enter a string: ");
scanf("%s",&inp);
for (length = 0; inp[length] != ''; ++length)
{
inp[length] = tolower(inp[length]);
}
for (int i=0;i<=length;i++)
{
checkcon(inp[i],con,conno);
checkvow(inp[i],vow,vowno);
}
printf("Consonants: %sn",con);
printf("Number of consonants: %dn",conno);
printf("Vowels: %sn",vow);
printf("Number of vowels: %d",vowno);
return 0;
}
</code>
<code>#include <stdio.h> #include <string.h> #include <ctype.h> void append(char string[] , char letter) { char arr[] = {letter , ''}; strcat(string,arr); } void checkcon(char letter,char string[] , int counter) { char consonants[] = "bcdfghjklmnpqrstvwxyz"; for (int i=0;i<=20;i++) { if (letter == consonants[i]) { append(string,letter); counter = counter + 1; printf("Con : %dn",counter); } } } void checkvow(char letter,char string[],int counter) { char vowels[] = "aeiou"; for (int i=0;i<=4;i++) { if (letter == vowels[i]) { append(string,letter); counter = counter + 1; printf("Vow : %dn",counter); } } } int main(void) { char inp[5000] = {0}; char vow[5000] = {0}; char con[5000] = {0}; int vowno,conno = 0; int length; printf("Enter a string: "); scanf("%s",&inp); for (length = 0; inp[length] != ''; ++length) { inp[length] = tolower(inp[length]); } for (int i=0;i<=length;i++) { checkcon(inp[i],con,conno); checkvow(inp[i],vow,vowno); } printf("Consonants: %sn",con); printf("Number of consonants: %dn",conno); printf("Vowels: %sn",vow); printf("Number of vowels: %d",vowno); return 0; } </code>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void append(char string[] , char letter)
{
    char arr[] = {letter , ''};
    strcat(string,arr);
}
void checkcon(char letter,char string[] , int counter)
{
    char consonants[] = "bcdfghjklmnpqrstvwxyz";
    for (int i=0;i<=20;i++)
    {
        if (letter == consonants[i])
        {
            append(string,letter);
            counter = counter + 1;
            printf("Con : %dn",counter);
        }
    }
}
void checkvow(char letter,char string[],int counter)
{
    char vowels[] = "aeiou";
    for (int i=0;i<=4;i++)
    {
        if (letter == vowels[i])
        {
            append(string,letter);
            counter = counter + 1;
            printf("Vow : %dn",counter);
        }
    }
}
int main(void)
{
    char inp[5000] = {0};
    char vow[5000] = {0};
    char con[5000] = {0};
    int vowno,conno = 0;
    int length;
    printf("Enter a string: ");
    scanf("%s",&inp);
    for (length = 0; inp[length] != ''; ++length)
    {
        inp[length] = tolower(inp[length]);
    }
    for (int i=0;i<=length;i++)
    {
        checkcon(inp[i],con,conno);
        checkvow(inp[i],vow,vowno);
    }
    printf("Consonants: %sn",con);
    printf("Number of consonants: %dn",conno);
    printf("Vowels: %sn",vow);
    printf("Number of vowels: %d",vowno);
    return 0;
}

Output :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Enter a string: String
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Consonants: strng
Number of consonants: 0
Vowels: i
Number of vowels: 15080
</code>
<code>Enter a string: String Con : 1 Con : 1 Con : 1 Vow : 15081 Con : 1 Con : 1 Consonants: strng Number of consonants: 0 Vowels: i Number of vowels: 15080 </code>
Enter a string: String
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Consonants: strng
Number of consonants: 0
Vowels: i
Number of vowels: 15080

I do not understand why my counter does not start from zero and increase sequentially.

I tried it with all the alphabets and this is the output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Enter a string: abcdefghijklmnopqrstuvwxyz
Vow : 15081
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Con : 1
Con : 1
Consonants: bcdfghjklmnpqrstvwxyz
Number of consonants: 0
Vowels: aeiou
Number of vowels: 15080
</code>
<code>Enter a string: abcdefghijklmnopqrstuvwxyz Vow : 15081 Con : 1 Con : 1 Con : 1 Vow : 15081 Con : 1 Con : 1 Con : 1 Vow : 15081 Con : 1 Con : 1 Con : 1 Con : 1 Con : 1 Vow : 15081 Con : 1 Con : 1 Con : 1 Con : 1 Con : 1 Vow : 15081 Con : 1 Con : 1 Con : 1 Con : 1 Con : 1 Consonants: bcdfghjklmnpqrstvwxyz Number of consonants: 0 Vowels: aeiou Number of vowels: 15080 </code>
Enter a string: abcdefghijklmnopqrstuvwxyz
Vow : 15081
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Con : 1
Con : 1
Vow : 15081
Con : 1
Con : 1
Con : 1
Con : 1
Con : 1
Consonants: bcdfghjklmnpqrstvwxyz
Number of consonants: 0
Vowels: aeiou
Number of vowels: 15080

I had referred to this since I was using a for loop to check for consonants and vowels instead of a function but I still did not get anything right. My question is quite different from this and from this because neither of the questions use a separate function to check for consonants and vowels and neither question have more details on how their counter is not working.

The problem focuses on why the counter was not working, this has been fixed by initialising the variables and assigning the return value of the function to the respective variables(change return type to int and return ‘counter’ after the loop).

10

The syntax of variable definition is not as you think it is.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int vowno,conno = 0;
</code>
<code>int vowno,conno = 0; </code>
int vowno,conno = 0;

Means:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int vowno; // define, but not initialize
int conno = 0; // define and initialize
</code>
<code>int vowno; // define, but not initialize int conno = 0; // define and initialize </code>
int vowno;     // define, but not initialize
int conno = 0; // define and initialize

If you want to define and initialize both variables in one line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int vowno = 0, conno = 0;
</code>
<code>int vowno = 0, conno = 0; </code>
int vowno = 0, conno = 0;

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật