show some operations on strings without using inbuilt methods of microsoft in C#


Category: C# Tags: C#, Interview Question

1. remove special characters 2. string is palindrome or not 3. reverse of a string 4. arrange elements in increasing order


Like 1 Person
Asked on 17 March 2014
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 133
Questions: 9
Given Best Solutions: 9 *

Answers:

Nikhil Joshi
I did these myself ... © nikhil joshi :P
//remove spcl charactors
        string s = "Pa@ll&*Etech!nology@$ies";
        string s2 = string.Empty;
        char[] ca = new char[] {'@','&','*','!','$' };
        for (int i = 0; i < s.Length; i++)
        { 
        //    char c = Convert.ToChar(s[i]);
        //    int ccc=(int)c;
        //    if ((ccc >= 97 && ccc <= 122) || (ccc>= 65 && ccc<=90))
        //    {
        //        s2 += s[i];
        //    }
        // This commented is also do same which can be done by given below
            int k = ca.Length;
            for (int j = 0; j < ca.Length; j++)
            {
                if (s[i] == ca[j])
                {
                    break;
                }
                else
                {
                    if (j == (k - 1))
                    {
                        s2 += s[i];
                        break;
                    }
                }
            }
        }
        Response.Write(s2);
        //Now checking plaindrom
        string []s_ar = new string[]{"mom","na","dad"};
        string plaindrom = string.Empty;
        foreach (string aa in s_ar)
        {
            if (aa[0] == aa[aa.Length-1])
            {
                plaindrom += aa;
            }
        }
        Response.Write("<br>"+plaindrom+"<br>");
        //now reverse of a string
        string s3 = string.Empty;
        for (int i = s2.Length - 1; i >= 0; i--)
        {
            s3 += s2[i];
        }
        Response.Write(s3);
        // arrange charactors in incresing order
        s3 = "hfghdxhadxa";
        char []s4 = new char[s3.Length];
        s4=s3.ToArray();
        for (int i = 0; i < s4.Length; i++) //bubble sort
        {
            for (int j = 0; j < s4.Length - 1; j++)
            {
                if ((int)s4[j] > (int)s4[j+1])
                {
                    char new_c = s4[j+1];
                    s4[j+1] = s4[j];
                    s4[j] = new_c;
                }
            }
        }
        int d = 0;
        foreach (object o in s4)
        {
            Response.Write("<br>" + s4[d]);
            d++;
        }
Like 0 People on 17 March 2014

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x