Author Topic: [C#] String Case-Inversion  (Read 1508 times)

0 Members and 1 Guest are viewing this topic.

Offline xor

  • Peasant
  • *
  • Posts: 59
  • Cookies: 32
    • View Profile
[C#] String Case-Inversion
« on: May 06, 2015, 08:06:30 am »

"A string like this", will become, "a STRING LIKE THIS"
"A StRing LIKE this", will become, "a sTrING like THIS"
etc.


Code: [Select]

    public static class StringExtensions
    {
        public static string InvertCase(this string s)
        {
            var cArr = s.ToCharArray();


            for (var i = 0; i < cArr.Length; i++)
            {
                var cInt = (int)cArr[i];
                if ((cInt >= 65 && cInt <= 90) || (cInt >= 97 && cInt <= 122))
                {
                    cInt ^= 1 << 5;
                    cArr[i] = (char) cInt;
                }
            }


            return new string(cArr);
        }
    }

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: [C#] String Case-Inversion
« Reply #1 on: May 06, 2015, 09:03:10 am »
I am not an expert in C# but here is another way to do so using lambda expressions in C#

https://code.hackerearth.com/5cd740w

Code: [Select]
using System;
using System.Linq;

class StringInversion
{
    public static void Main()
    {   
        string data = "i Am A mONkeY";
       
        string invStr = new string(data.Select(chr => char.IsLetter(chr) ? (char.IsLower(chr) ? char.ToUpper(chr) : char.ToLower(chr)) : chr).ToArray());
       
        Console.WriteLine(invStr);
    }
}
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline xor

  • Peasant
  • *
  • Posts: 59
  • Cookies: 32
    • View Profile
Re: [C#] String Case-Inversion
« Reply #2 on: May 06, 2015, 10:33:01 am »
That definitely looks simpler.
I did a benchmark on the functions to see how they fared.


Testing code here:
https://code.hackerearth.com/f0bf55Y


Output:
F1 here is the method I posted.
F2 is the method using LINQ.


Code: [Select]

Running 10 character test for 1000000 iterations
[F1-TEST] 70.2386 milliseconds
[F2-TEST] 664.5493 milliseconds


Running 11 character test for 1000000 iterations
[F1-TEST] 73.1339 milliseconds
[F2-TEST] 705.5209 milliseconds


Running 12 character test for 1000000 iterations
[F1-TEST] 77.119 milliseconds
[F2-TEST] 750.4108 milliseconds


Running 13 character test for 1000000 iterations
[F1-TEST] 81.6811 milliseconds
[F2-TEST] 793.1807 milliseconds


Running 14 character test for 1000000 iterations
[F1-TEST] 95.1609 milliseconds
[F2-TEST] 832.2692 milliseconds


Running 15 character test for 1000000 iterations
[F1-TEST] 100.7157 milliseconds
[F2-TEST] 877.7269 milliseconds


Running 16 character test for 1000000 iterations
[F1-TEST] 102.0455 milliseconds
[F2-TEST] 882.1031 milliseconds


Running 17 character test for 1000000 iterations
[F1-TEST] 107.5085 milliseconds
[F2-TEST] 1011.3637 milliseconds


Running 18 character test for 1000000 iterations
[F1-TEST] 113.2146 milliseconds
[F2-TEST] 1056.1293 milliseconds


Running 19 character test for 1000000 iterations
[F1-TEST] 117.2205 milliseconds
[F2-TEST] 1095.9465 milliseconds




Conclusion:


If you want performance and backward compatibility, go bit-wise operators, if you want simplicity of understanding, go LINQ.


-- xor

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: [C#] String Case-Inversion
« Reply #3 on: May 06, 2015, 03:51:38 pm »
I never said my method is faster. Its just that its easier and more readable thats all. Sometimes losing efficiency over readability is better :D
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline xor

  • Peasant
  • *
  • Posts: 59
  • Cookies: 32
    • View Profile
Re: [C#] String Case-Inversion
« Reply #4 on: May 07, 2015, 04:40:57 am »
This is why the philosophy of encapsulation exists.


A person does not need to know the internals of a function, so long as that function takes their input and gives them the expected output.


Exactly how you would use these functions, if they were placed in a library.


-- xor

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: [C#] String Case-Inversion
« Reply #5 on: May 23, 2015, 03:51:06 am »
My revision is faster..
Code: [Select]
public static unsafe string InvertCase2(this string s)
{
      char* pch = stackalloc char[s.Length];
      for (int i = 0; i < s.Length; ++i)
        pch[i] = (char)(s[i] ^ 0x20);
      return new string(pch);
}

I don't know why you hardcode 1 << 5, when you can just XOR 32...
« Last Edit: May 23, 2015, 03:58:12 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline xor

  • Peasant
  • *
  • Posts: 59
  • Cookies: 32
    • View Profile
Re: [C#] String Case-Inversion
« Reply #6 on: May 27, 2015, 06:58:04 pm »
Not sure why I did that either. It was a case of "it works", and left it at that.
+1 for the code. Though I'm not a fan of using unsafe, where possible.

I completely let binary slip my mind when writing this.
Posting a tutorial so I don't forget again.

-- xor
« Last Edit: May 27, 2015, 07:31:53 pm by xor »

Offline x0nic

  • Peasant
  • *
  • Posts: 51
  • Cookies: 5
    • View Profile
Re: [C#] String Case-Inversion
« Reply #7 on: May 28, 2015, 08:39:32 pm »
Ah yeah some nice suggestions in here. However, is there any particular use for such a function except for passing some "hello world - chapter 2" tutorials? Excuse my lack of knowledge

Offline xor

  • Peasant
  • *
  • Posts: 59
  • Cookies: 32
    • View Profile
Re: [C#] String Case-Inversion
« Reply #8 on: May 29, 2015, 03:15:37 am »
I cannot think of an actual real life use-case.


Perhaps, if you were writing a spell checker and someone had their CAPS LOCK STUCK ON. yOU COULD ... you get the point... you could detect the percentage rate of capitalisation, disable CAPS LOCK, and invert the string they just typed.


-- xor