String based program

String program #10 (Count Characters and its category)

Enter string: Liveviar 23@#$
l is constants
i is vowel
v is constants
e is vowel
v is constants
i is vowel
a is vowel
r is constants
  is specialCharacter
2 is numbers
3 is numbers
@ is specialCharacter
# is specialCharacter
$ is specialCharacter

Total number of occurrences: 14
Vowels: 4
Constants: 4
Numbers: 2
Special Character: 4

The program for the string is written in C# programming language and will accept a string as input. The logic is to count characters occurrence and its category.
 
String based program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code the program.

Practical Implementation:

using System;

namespace patternProblem.String
{
    class String10     //count characters and its category
    {
        static void Main(string[] args)
        {
            Console.Write("Enter string: ");
            string str = Console.ReadLine();
            int vowels = 0, constants = 0, numbers = 0, specialCharacter = 0;

            char[] ch = str.ToLower().ToCharArray();
            for (int i = 0; i <= str.Length - 1; i++)
            {
                if ((ch[i] == 'a')||(ch[i] == 'e')||(ch[i] == 'i')||(ch[i] == 'o')||(ch[i] == 'u'))
                {
                    vowels++;
                    Console.WriteLine("{0} is vowel", ch[i]);
                }
                else if (ch[i] >= 48 && ch[i] <= 57)
                {
                    numbers++;
                    Console.WriteLine("{0} is numbers", ch[i]);
                }
                else if (ch[i] >= 97 && ch[i] <= 122)
                {
                    constants++;
                    Console.WriteLine("{0} is constants", ch[i]);
                }
                else
                {
                    specialCharacter++;
                    Console.WriteLine("{0} is specialCharacter", ch[i]);
                }
            }

            Console.WriteLine("\nTotal number of occurrences: {0}", str.Length);
            Console.WriteLine("Vowels: {0}", vowels);
            Console.WriteLine("Constants: {0}", constants);
            Console.WriteLine("Numbers: {0}", numbers);
            Console.WriteLine("Special Character: {0}", specialCharacter);

            Console.ReadKey();
        }
    }
}

Output:
The input string1 here is “Liveviar 23@#$”. So, based on the input, program will count all characters occurrence based on its category. The output is shown below:

Enter string: Liveviar 23@#$
l is constants
i is vowel
v is constants
e is vowel
v is constants
i is vowel
a is vowel
r is constants
  is specialCharacter
2 is numbers
3 is numbers
@ is specialCharacter
# is specialCharacter
$ is specialCharacter

Total number of occurrences: 14
Vowels: 4
Constants: 4
Numbers: 2
Special Character: 4
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table String based program

Previous - String based program #9

Next – String based program #11

Click imagination hunt to read latest blogs.


Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions