How to toggle character case in PHP without strlower, strupper & ucfirst (inbuilt) and Array function

By

April 2, 2012Interview PHP1 Comment

Toggling between upper and lower case letter is quite challenging.  There is a PHP inbuilt function’s which can do this operation  PHP: strtoupper – Manual , PHP strtolower() Function & PHP: ucfirst – Manual , the real challenge would be you have to toggle case of the given word without using PHP built in function and Array function. You might have got a chance to solve these kind of programs in the interview, some will succeed and some will fail.  Toggling between character case can be achieved in simpler way and steps are shown below,

  • Get the string input on which the case should be toggled
  • Calculate the length of the string
  • Iterate to get the ASCII code of each string
  • Check whether the ASCII code is range between [64-91] which is upper case
  • Check whether the ASCII code is range between [96-123] which is lower case
  • If  its upper case add 32 to make it lower case
  • If  its lower case subtract 32 to make it upper case
  • Check the condition only for alphabets
  • Finally concatenate the string

The below code snippets shows the PHP code to toggle character case without Array and strlower, strupper & ucfirst (inbuilt) function.

One Response to “How to toggle character case in PHP without strlower, strupper & ucfirst (inbuilt) and Array function”
  1. Vineet says:

    Its nice…. 🙂

Leave a Reply