Sunday, October 25, 2015

sort string and number

 private void sortnumber(string p)
    {
        char[] a =p.ToCharArray();
        
        int temp = 0;
        for (int i = 0; i < p.Length; i++)
        {
            for (int j = i + 1; j < p.Length; j++)
            {
                if (a[i] < a[j])
                {
                    temp = a[j];
                    a[j] = a[i];
                    a[i] = Convert.ToChar(temp);
                }
            }
            
                Response.Write(a[i]);
        }

Capital to small and viceversa

private string conversion(string p)
    {
        char[] a = p.ToCharArray();
        if (a.Length > 1)
        {
            if (char.IsLower(a[0]))
            {
                a[0] = char.ToUpper(a[0]);
            }
            for (int i = 1; i < a.Length; i++)
            {
                if(a[i-1]==' ')
                {
                    if (char.IsLower(a[i]))
                    {
                        a[i] = char.ToUpper(a[i]);
                    }
                    else if (char.IsUpper(a[i]))
                    {
                        a[i] = char.ToLower(a[i]);
                    }
                }
            }
        }
       
        return new string(a);
    }

Character and word count in c#

 private void character(string p)
    {
        string s = p;
        int count = 0;
        try
        {
        char c=Convert.ToChar(TextBox2.Text);
       
            foreach (char item in s)
            {
                if (item == c)
                {
                    count++;
                }
            }
            Response.Write("no of " + c + " are " + count);
        }
        catch (Exception )
        {
            Response.Write("invalid format given");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        wordcount(TextBox1.Text);
    }

    private void wordcount(string p)
    {
        int count = 0;
        string s = p;
        string c = TextBox2.Text;
        foreach (Match item in Regex.Matches(s,c,RegexOptions.IgnoreCase))
        {
            count++;
        }
     
            Response.Write("word" + c + "count is " + count);
        }