Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
345
Q:

Can someone share a PHP program that converts a string to uppercase without using the built-in functions?

Hey guys,

I'm fairly new to PHP and currently working on a small project. I'm trying to find a way to convert a string to uppercase in PHP, but I want to do it without using the built-in functions like `strtoupper()` or `mb_strtoupper()`. I know these functions exist, but I'm specifically looking for an alternative solution.

I've been searching online and reading through the PHP manual, but I haven't been able to find a suitable solution yet. I'm wondering if any of you have ever come across a program or code snippet that achieves this without using built-in functions.

If you have any ideas or suggestions on how I could approach this problem, I would really appreciate your help. Thanks in advance!

Best regards,
[Your Name]

All Replies

roosevelt25

Hey [Your Name],

I completely understand your desire to find an alternative solution for converting a string to uppercase without using the built-in functions. While it's true that functions like `strtoupper()` already exist, sometimes exploring alternative methods can be an interesting challenge.

One way to achieve this is by iterating over each character in the string and converting it to its uppercase representation based on its ASCII value. Here's an example code snippet that might help you get started:

php
// Define the string you want to convert
$string = "hello world";

// Create an empty variable to store the uppercase version
$uppercaseString = "";

// Iterate over each character in the string
for ($i = 0; $i < strlen($string); $i++) {
$asciiValue = ord($string[$i]);

// Check if the character is a lowercase letter
if ($asciiValue >= 97 && $asciiValue <= 122) {
// Convert it to uppercase by subtracting 32 from the ASCII value
$uppercaseCharacter = chr($asciiValue - 32);
} else {
// Leave the character unchanged if it's not a lowercase letter
$uppercaseCharacter = $string[$i];
}

// Append the converted character to the uppercaseString variable
$uppercaseString .= $uppercaseCharacter;
}

// Output the resulting uppercase string
echo $uppercaseString;


This code converts each character to uppercase by checking its ASCII value and manipulating it accordingly. It's worth mentioning that this method only works for English lowercase letters.

I hope this helps! Let me know if you have any further questions or concerns.

Best regards,
[Your Name]

corwin.jedidiah

Hey there,

I can understand your curiosity in finding an alternative method to convert a string to uppercase without relying on built-in functions. It's always exciting to explore different approaches to solving problems!

One possible solution, which is slightly different from the previous suggestion, is to utilize the ASCII values of characters and perform manipulations to convert lowercase letters to uppercase. Here's a code snippet that you can try out:

php
// Define the string you want to convert
$inputString = "hello world";

// Create an empty string to store the uppercase version
$upperString = "";

// Iterate through each character in the input string
for ($i = 0, $length = strlen($inputString); $i < $length; $i++) {
$char = $inputString[$i];
$asciiValue = ord($char);

// Check if the character is a lowercase letter
if ($asciiValue >= 97 && $asciiValue <= 122) {
// Add the difference between lowercase and uppercase ASCII values
$char = chr($asciiValue - 32);
}

// Append the updated character to the upperString variable
$upperString .= $char;
}

// Display the resulting uppercase string
echo $upperString;


In this code, we iterate through each character in the input string and examine its ASCII value. If the character falls within the range of lowercase letters (97-122), we use the ASCII value difference between lowercase and uppercase letters (32) to convert it to uppercase.

Do give this approach a try and see if it meets your requirements. If you have any further questions or need more assistance, feel free to ask!

Best regards,
[Your Name]

New to LearnPHP.org Community?

Join the community