This doesn't show you how to make the function itself, so I'll show you a way how to do it whilst making everything yourself. It's fairly complex for what it does, and feel free to ask questions, but in short, it checks for each character in the string, and if its ASCII value is greater than 96 ('a' and above) and less than 123 ('z' and below), then it will take 32 from its value (if you look on the ASCII table, uppercase equivalents are 32 less in decimal) and add that to the string we made earlier; if not, then it will simply add the character to the string as is, as changing the value of a character that isn't a lowercase letter will make it a different character that is nothing to do with what it was before (again, look at the ASCII table and you'd see why). Make sure to include the header whilst doing this, or you won't be able use std::string. #include using namespace std; string toUpper(string str) { string newString; for (int i = 0; i < str.length(); i++) { if (str.at(i) > 96 && str.at(i) < 123) { newString += str.at(i) - 32; } else { newString += str.at(i); } } return newString; }
Also, I'm fairly new to C++ programming, but I've had experience in some other programming languages, and I have tested it and it has worked so far. If you're working with other datatypes (like char* or char[]) then I wouldn't imagine that the function would work, but you should probably use std::string instead anyway, instead of the C alternatives.
Also again, you can alter this to make a toLower() function. Simply replace: if (str.at(i) > 96 && str.at(i) < 123) { with: if (str.at(i) > 64 && str.at(i) < 91) { and replace: newString += str.at(i) - 32; with: newString += str.at(i) + 32; You might want to look up the ASCII table and find out how it works, note that I'm using decimal ASCII values, not hexadecimal, although it should still work if you convert the numbers to hexadecimal, decimal is much more readable (as that's what you count with)
Listen man, I'm gonna comment on this video, because you helped me save my semester. Basically the teacher said she'd give me my 10 missing points to pass, if I could come up with a toUpper or toLower solution. THANK YOU!
If you are reading this, thank you!! your code is so much simplier than the one I used to use.
A big like for you !!!! Your codes are much more useful than the other codes on web!!! The other codes on web are just too complex!
Awesome dude, really helps me out, It's nice to see this from a professional and not from a noob...
Thank you so much . I couldn't find this anywhere on the web and I needed it so dearly for my project !
Thanks a million. This just saved me from so much work.
This doesn't show you how to make the function itself, so I'll show you a way how to do it whilst making everything yourself. It's fairly complex for what it does, and feel free to ask questions, but in short, it checks for each character in the string, and if its ASCII value is greater than 96 ('a' and above) and less than 123 ('z' and below), then it will take 32 from its value (if you look on the ASCII table, uppercase equivalents are 32 less in decimal) and add that to the string we made earlier; if not, then it will simply add the character to the string as is, as changing the value of a character that isn't a lowercase letter will make it a different character that is nothing to do with what it was before (again, look at the ASCII table and you'd see why).
Make sure to include the header whilst doing this, or you won't be able use std::string.
#include
using namespace std;
string toUpper(string str) {
string newString;
for (int i = 0; i < str.length(); i++) {
if (str.at(i) > 96 && str.at(i) < 123) {
newString += str.at(i) - 32;
} else {
newString += str.at(i);
}
}
return newString;
}
Also, I'm fairly new to C++ programming, but I've had experience in some other programming languages, and I have tested it and it has worked so far. If you're working with other datatypes (like char* or char[]) then I wouldn't imagine that the function would work, but you should probably use std::string instead anyway, instead of the C alternatives.
Also again, you can alter this to make a toLower() function.
Simply replace:
if (str.at(i) > 96 && str.at(i) < 123) {
with:
if (str.at(i) > 64 && str.at(i) < 91) {
and replace:
newString += str.at(i) - 32;
with:
newString += str.at(i) + 32;
You might want to look up the ASCII table and find out how it works, note that I'm using decimal ASCII values, not hexadecimal, although it should still work if you convert the numbers to hexadecimal, decimal is much more readable (as that's what you count with)
This video saved me hours of work. Thanks!!!
It was so simple to do it! :)) Thank you!
Now i have peace, thanks man!
Awesome, thanks a million!!!!
Listen man, I'm gonna comment on this video, because you helped me save my semester. Basically the teacher said she'd give me my 10 missing points to pass, if I could come up with a toUpper or toLower solution. THANK YOU!
Thank you. You the REAL MVP
Gracias!! Me sirvió muchisimo.
Thanks
i try to run it but i got an error,a red crooked line under the { void convert. any advice?
And what if one needs to apply the same to *char* variables?
You need to get array of char and then index
if I want to upper two strings in a for loop, how could I do that? Also just the first letter tho, thanks.
I guess you would use substr
this code didnt work for me... what IDE and what compiler are you using?
Suppose i want to make huge size of Alphabet ??? What program would be that
Can someone explain me what line 4-21 mean?I am new at programming
OMG, thanks!
Sucks that tolower/toupper doesn't work on strings. We have to resort to this crap...
Someone explain what string& does. Not in advanced terms such as 'pointers' and shit cause that assume I know what those are in the first place.
Thank you omg
This function does not work for me when string involves both types of letters. Ex: HeLlo
Thaaaaanks a lot
Thanks you so much
This shows "use of undeclared identifier 'i' "
YOU ARE GOOD
Praise the sun !!!
thank you very much.Great