Q:

C# program to count vowels in character array using pointers

0

C# program to count vowels in character array using pointers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to count vowels in character array using pointers is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to count vowels from character array using pointers.

using System;

class UnsafeEx
{
    static unsafe void Main(string[] args)
    {
        int loop = 0;
        int countVowels=0;

        char[] str = { 'i','n','c','l','u','d','e','h','e','l','p'};
        
        fixed(char *ptr = str)
        for (loop = 0; loop<str.Length; loop++)
        {
            if ((*(ptr + loop) == 'a') || (*(ptr + loop) == 'e') || (*(ptr + loop) == 'i') || (*(ptr + loop) == 'o') || (*(ptr + loop) == 'u'))
                countVowels++;
        }
        Console.WriteLine("Total Vowels are: "+countVowels);
    }
}

Output:

Total Vowels are: 4
Press any key to continue . . .

Explanation:

In the above program, we created class UnsafeEx that contains the Main() method, here we used the unsafe keyword with the Main() method to define the unsafe method that can use pointers.

In the Main() method, we created an array of characters elements then we assign the address of the array to the pointer and then count the vowels from the character array using the pointer. After that count of vowels will be printed on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now