- Define the function IsNumeric which returns 1 if the character is numeric, 0 otherwise
- Define the function IsVowel which returns 1 if the character is a vowel, 0 otherwise
(Vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}).
Then, write a program that asks the user to enter a paragraph, it displays it such that the numbers appear in Blue color and the vowels appear in Red color.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#define SIZE 20
void SetColor(int ForgC)
{
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
int IsNumeric(char c)
{
return (c >= '0' && c <= '9');
}
int IsVowel(char c)
{
return (c== 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void print(char paragraph[SIZE])
{
int i;
for (i = 0; paragraph[i]; i++)
{
if (IsNumeric(paragraph[i]))
SetColor(4); // for red
else if (IsVowel(paragraph[i]))
SetColor(1); // for blue
else
SetColor(0); // for black
printf("%c", paragraph[i]);
}
}
void main()
{
char paragraph[SIZE];
printf("enter a paragraph: ");
gets(paragraph);
print(paragraph);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using heap-sort algorithm