+ 1
tolower question
Is it possible to make an entire string lowercase without having to loop through each char? https://code.sololearn.com/cJiG9JYPLwZI/?ref=app Somebody made something like this in a discord server I'm in. It simply generates the random capitalization used in a certain spongebob meme. I decided to remake it out of boredom. I decided to make the entire string lowercase to completely randomize the capitalization. Is there a way to make the entire string lowercase without having to loop through the characters?
4 Respuestas
+ 4
All other languages which have a tolower() essentially does the same thing anyway. Ensuring that each character is lowercase/uppercase is an O(n) task, so iteration is mandatory (either you do it or the built-in function does). The SO link provided contains the example using std::transform, which is the best you can get if you don't want to write loops. I'm wondering if regex can be used to achieve the task.
+ 4
An old-school yet efficient way to do so is catching the uppercase characters while getting them from the input stream as follow
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s = "";
char c;
while ( cin.get(c) ) {
if ( c == '\n' )
break;
s += tolower(c);
}
cout << s;
}
this way, the program gets one line of string, converts it to proper lowercase equivalent, and then appends it to initialized string. O(0)! ;)
Live version: https://rextester.com/KAVJ12865
+ 3
Welp
Guess I gotta Iterate.
Thanks Arushi Singhania