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.
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
Keep learning and sharing...
No comments:
Post a Comment