Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

2.Write a program in C# Sharp to count a total number of alphabets, digits and special characters in string.

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace program3

{

public class program3

{

public static void Main()

{

string str;

int alp, digit, splch, i, l;

alp = digit = splch = i = 0;

Console.Write("\n\nCount total number of alphabets, digits and special characters

:\n");

Console.Write("

Console.Write("Input the string : ");

str = Console.ReadLine();

l = str.Length;

/* Checks each character of string*/

while (i < l)

{

\n");

if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))

{

alp++;

}

else if (str[i] >= '0' && str[i] <= '9')

{

digit++;

}

else

{

splch++;

}

i++;

}

Console.WriteLine("Length of the string is : "+l);

Console.WriteLine ("Number of Alphabets in the string is :"+alp);

Console.WriteLine ("Number of Digits in the string is : "+digit);

Console.WriteLine ("Number of Special characters in the string is :"+splch);

Console.ReadKey();

}

}

}

Post a Comment

0 Comments