Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

C# Convert to lower case and upper case manually

views
     
TSFlierMate4
post Mar 5 2023, 02:31 PM, updated 3y ago

Getting Started
**
Validating
90 posts

Joined: Jan 2023
This is in C# , but also applicable in other languages:

QUOTE
AND,0xDF as to upper and OR,0x20 as lower, or XOR,0x20 for toggling.


CODE
       static string UCase(string input)
       {
           string output=null;
           for (int i = 0; i<input.Length; i++)
           {
               output += (char)(input[i] & 0xDF);
           }
           return output;
       }

       static string LCase(string input)
       {
           string output = null;
           for (int i = 0; i < input.Length; i++)
           {
               output += (char)(input[i] | 0x20);
           }
           return output;
       }

       static string ToggleCase(string input)
       {
           string output=null;
           for (int i = 0; i < input.Length; i++)
           {
               output += (char)(input[i] ^ 0x20);
           }
           return output;
       }

       static void Main(string[] args)
       {
           Console.WriteLine(UCase("Hello World"));
           Console.WriteLine(LCase("Hello World"));
           Console.WriteLine(ToggleCase("Hello World"));
           Console.ReadKey();
       }


Can you explain how it works?

This post has been edited by FlierMate4: Mar 5 2023, 02:32 PM
flashang
post Mar 5 2023, 05:01 PM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


The code is not complete.
It should compare the range before upper / lower case.
Otherwise it will mess-up those string contains numbers & symbols.

smile.gif



This post has been edited by flashang: Mar 5 2023, 05:01 PM
TSFlierMate4
post Mar 5 2023, 05:47 PM

Getting Started
**
Validating
90 posts

Joined: Jan 2023
QUOTE(flashang @ Mar 5 2023, 05:01 PM)
It should compare the range before upper / lower case.
*
You're right, should evaluate if char is greater than 'A'/'a' or less than 'Z'/'z'.

sad.gif
WongGei
post Mar 5 2023, 09:25 PM

Regular
******
Senior Member
1,206 posts

Joined: Dec 2007
From: Kuala Lumpur
This only work on ASCII code, ...

 

Change to:
| Lo-Fi Version
0.0145sec    0.37    5 queries    GZIP Disabled
Time is now: 24th December 2025 - 12:42 PM