This is the start of a series of about 40 programming exercise for beginners. I hope that they will be fun for novice programmers. Try to solve all of them. Have fun 🙂
This is a classical coding problem, and many students around the world solve it on a daily basis. There will be a solution under the problem, but its a good idea to try to first solve it on your own. The solution will be in the C# programming language.
Write a function that returns the number (count) of vowels in the given string.
We will consider a, e, i, o, u as vowels (but not y).
Scroll down for the solution.
public static int GetVowelCount(string str) { //first we are going to reserve a piece of memory //it will be an integer and we will name it vowelCount int vowelCount = 0; //this ToLower() function will make all the letters lowercase str = str.ToLower(); //then we will go through every character in the string //and if one of the characters is a Vowel we will increase //the the number stored in (memory)vowelCount, by using //++ operator (it adds +1) to our integer variable. // "||" is the OR operator. So we are saying that //if c is equal to 'a' or if c is equal to 'e' and so on... foreach (char c in str) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { vowelCount++; } } return vowelCount; }
There is many way to solve this coding problem, also there is many programming languages. I will use C#, because is a C based language, and also is widely used. For example Unity3d (the most popular game engine) uses C#. Also with C# you can write for PC, Linux, MacOS, iPhone, Android, and other devices by using .NET Core.