Write a program that first reads an integer for the array size,
then reads characters into the array, and displays the consonants (i.e., a character
is displayed only if it a consonant). (Hint: Read a character and store it to an array
if it is not a vowel. If the character is a vowel, discard it. After the input, the array
contains only the consonants.)
#include <iostream>
bool is_consonant(const char * c)
{
char consonant[] = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";
if (strpbrk(c, consonant) != NULL)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
char* arr = nullptr;
int arr_size = 0;
int size = 0;
std::cout << "Enter size of array : ";
std::cin >> size;
for (int i = 0; i < size; i++)
{
std::cout << "Enter symbol number " << i + 1 << " : ";
char tmp[2];
std::cin >> tmp;
if (is_consonant(tmp))
{
char* tmp_arr = new char[arr_size + 1];
for (int j = 0; j < arr_size; j++)
{
tmp_arr[j] = arr[j];
}
tmp_arr[arr_size] = tmp[0];
delete[]arr;
arr = tmp_arr;
arr_size++;
}
}
for (int i = 0; i < arr_size; i++)
{
std::cout << arr[i] << "\t";
}
std::cout << std::endl;
system("pause");
return 0;
}
Comments
Leave a comment