Conversion of string to upper case without inbuilt methods
You need to execute ord() for each character of your input string. instead of the input string:
def uppercase(str_data):
return ''.join([chr(ord(char) - 32) for char in str_data if ord(char) >= 65])
print(uppercase('abcdé--#'))
>>> ABCDÉ
Without join:
def uppercase(str_data):
result = ''
for char in str_data:
if ord(char) >= 65:
result += chr(ord(char) - 32)
return result
print(uppercase('abcdé--#λ'))
>>> ABCDÉΛ
How to convert lowercase string to uppercase string without toUppercase() function
function alpha()
{
var a=prompt("Enter String: ");
var output="";
for(var x=0;x<a.length;x++)
output+=String.fromCharCode(a.charCodeAt(x)>96 && a.charCodeAt(x)<123 ? a.charCodeAt(x)-32: a.charCodeAt(x))
return output;
}
console.log("--" + alpha());
Just get the ascii code for each char, and if it ranges between 97 to 122 ( a to z ) deduct 32 from val to get the corresponding upper case char. Hope this helps.
How can i convert to upper case manually ?(Javascript)
Here is a function that does it (very old-school and manual) :
- Loop through the string
- If you encounter a lower case character, in other words, if you are at a character whose ASCII code is in the range [97,122], which is the range of the lower case alphabet characters, subtract 32 from it's ASCII code, because the difference between a lower case alpha-char and it's upper case form in ASCII is 32, then add it to a new string.
- Else, add the character as is.
And as @georg, who is German (German alphabets include accented letters) pointed out, I added an update to include them.
Their range is [224,255], and the difference between each one and its upper-case form is also 32, so, no need for an else if :
function myToUpperCase(str) { var newStr = ''; for (var i=0;i<str.length;i++) { var thisCharCode = str[i].charCodeAt(0); if ((thisCharCode>=97 && thisCharCode<=122)||(thisCharCode>=224 && thisCharCode<=255)) { newStr += String.fromCharCode(thisCharCode - 32); } else { newStr += str[i]; } } return newStr;}console.log(myToUpperCase('helLo woRld!')); // => HELLO WORLD!console.log(myToUpperCase('üñïçødê')); // => ÜÑÏÇØDÊ
Java - Convert lower to upper case without using toUppercase()
Looks like homework to me, Just a hint. You are printing string a
whereas you are modifying the char
type aChar
, its not modifying the original string a
. (Remember strings are immutable).
How i can Capitalize without using toUpperCase
You can use fromCharCode and charCodeAt to switch between lower and upper letter:
function capitalize(word) { var firstChar = word.charCodeAt(0); if (firstChar >= 97 && firstChar <= 122) { return String.fromCharCode(firstChar - 32) + word.substr(1); } return word;}
alert( capitalize("abcd") + "\n" + capitalize("ABCD") + "\n" + capitalize("1bcd") + "\n" + capitalize("?bcd"));
Related Topics
How to Export a Table Dataframe in Pyspark to Csv
How to Iterate Through Cur.Fetchall() in Python
How to Change a Two Dimensional Array to One Dimensional
Calculate Sklearn.Roc_Auc_Score for Multi-Class
How to Calculate Range Between the Dataframe Values Using Python
How to Get the Neighboring Elements in a Numpy Array With Taking Boundaries into Account
How to Get Current Cpu and Ram Usage in Python
Swap First and Last Digits of a Number( Using Loops)
How to Redeem Nitro Gifts Automatically With Discord.Py (Self-Bot)
Plotly: Plot Multiple Figures as Subplots
How to Convert List into String With Quotes in Python
How to Count the Number of Files in a Directory Using Python
Google Chrome Closes Immediately After Being Launched With Selenium
How to Format an Integer to a Two Digit Hex
Get Max Value Comparing Multiple Columns and Return Specific Values
How to Convert an Integer to Time