Friday, November 30, 2012

c# - Convert a string to lowercase,uppercase without using builtin functions

LOWERCASE 

try
{
    string s = "This Is A Sentence!!!";
    char[] ca = s.ToCharArray();
    foreach (char c in ca)
    {
       if ((int)c < 91 && (int)c > 64)
          Console.Write(((char)((int)c + 32)));
       else
          Console.Write(c);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}















UPPERCASE

try
{
    string s = "This Is A Sentence!!!";
    char[] ca = s.ToCharArray();
    foreach (char c in ca)
    {
        if ((int)c < 123 && (int)c > 96)
           Console.Write(((char)((int)c - 32)));
        else
           Console.Write(c);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
 
     

1 comment:

Bhavna said...

thanks a lot
was searching for doing dis as it is an imp question in ma c# pprs.
thnx again :)