Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifing number of character/digits/special character in string

Tags:

string

c#

I want to calculate the summary of string in terms of number of alphabets, digits and special character in C#. For example:

String abc123$% should have summary like A3D3S2 (which means 3 Alphabet, 3 Digits and 2 Special character)

a34=$@ should have summary like A1D2S3 (which means 1 Alphabet, 2 Digits and 3 Special character)

a3b$s should have summary like A1D1A1S1A1 (which means 1 Alphabet, 1 Digits,1 Alphabet, 1 Special character,1 Alphabet)

Can anyone guide me how can write an algorithm which can perform the above task in a quick way? as I think if I search the string character by character, then it will take considerable amount of time. and I have a large dataset of strings.

like image 983
WiXXeY Avatar asked Nov 29 '25 14:11

WiXXeY


2 Answers

This works:

    static string GetSummary(string input)
    {
        var sb = new StringBuilder();

        string prevMode = "";
        string curMode = "";
        int sameModeCount = 0;

        for (int i = 0; i <= input.Length; ++i)
        {
            if (i < input.Length)
            {
                char c = input[i];
                if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z')
                {
                    curMode = "A";
                }
                else if ('0' <= c && c <= '9')
                {
                    curMode = "D";
                }
                else
                {
                    curMode = "S";
                }
            }
            else
            {
                curMode = "";
            }

            if (curMode != prevMode && prevMode != "")
            {
                sb.Append(prevMode);
                sb.Append(sameModeCount);

                sameModeCount = 0;
            }

            prevMode = curMode;
            ++sameModeCount;
        }

        return sb.ToString();
    }

Test:

    public static void Main()
    {
        Console.WriteLine(GetSummary("abc123$%"));
        Console.WriteLine(GetSummary("a34=$@"));
        Console.WriteLine(GetSummary("a3b$s"));
    }

Results:

A3D3S2
A1D2S3
A1D1A1S1A1
like image 94
Ripple Avatar answered Dec 02 '25 04:12

Ripple


With Linq, you can do like this :

string myinput = "abc123$%";
            int letter =0 , digit = 0, specialCharacter = 0;
            myinput.ToCharArray().ToList().ForEach(x =>
            {
                letter = Char.IsLetter(x) ? ++letter : letter;
                digit = Char.IsDigit(x) ? ++digit : digit;
                specialCharacter = !Char.IsLetterOrDigit(x) ? 
                                  ++specialCharacter : specialCharacter;
            });
            string formattedVal = String.Format("A{0}D{1}S{2}", letter, digit, 
                                                       specialCharacter);

You can directly use array in Linq ForEach without converting to list by :

Array.ForEach(myinput.ToCharArray(), x =>
            {
                letter = Char.IsLetter(x) ? ++letter : letter;
                digit = Char.IsDigit(x) ? ++digit : digit;
                specialCharacter = !Char.IsLetterOrDigit(x) ? ++specialCharacter : specialCharacter;
            });
            string formattedVal = String.Format("A{0}D{1}S{2}", letter, digit, specialCharacter);
like image 36
Akash KC Avatar answered Dec 02 '25 05:12

Akash KC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!