Dear Code Can you provide me code in C# for count the number of Capital,Small letter, vowel and how many number in given string?
Dear Topic Every one ask this in C# you are the one who ask me in SQL SERVER.Any ways query are given below.
Code Sample [C# Code]
using System;
using System.Text;
public class Program
{
public static void Main()
{
Console.WriteLine("Please enter a string");
string inputString = Console.ReadLine();
byte[] arrASCIIBytes = Encoding.ASCII.GetBytes(inputString);
//Encoding.ASCII.GetBytes
is used for convert the strin into byte Array
int smallCount = 0;
int capitalCount = 0;
int vowelCount = 0;
int numericCount=0;
foreach (byte ascii in arrASCIIBytes)
{
if (ascii >= 48 &&
ascii <= 57)
numericCount++;
else if (ascii >= 65 &&
ascii <= 90)
capitalCount++;
else if (ascii >= 97 && ascii <= 122)
smallCount++;
switch (ascii)
{
case 65:
case 69:
case 73:
case 79:
case 85:
case 97:
case 101:
case 105:
case 111:
case 117:
vowelCount++;
break;
}
}
Console.WriteLine("Number of
small letters in the entered string: " +
smallCount);
Console.WriteLine("Number of
cap letters in the entered string: " +
capitalCount);
Console.WriteLine("Number of
vowel letters in the entered string: " +
vowelCount);
Console.WriteLine("Number of Number
in the entered string: " +
numericCount);
}
}
Output
Please enter a stringTesting for count 123number,capital,small and vowel
Number of small letters in the entered string: 40
Number of cap letters in the entered string: 1
Number of vowel letters in the entered string: 14
Number of Number in the entered string: 3
Explanation
Here I am using Encoding.ASCII.GeBytes. Basically its convert the string into ASCII value and return the Byte Array.For this you have to use namespace System.Text. For Example we know the Ascii value of A is 65 so we have the check the condition from 65 to 90 for capital letter .Hope this will helpful for all my virtual world friends.
0 comments:
Post a comment