I wanted to write a function for tolower() in c++ without using STL. When I am giving small inputs like "Hello" , I am getting correct output with my code but when I am giving input with large paragraphs which has punctuations in it , then I am getting error. Can anyone help to fix and help to understand why I am getting Error?
My code :
#include <iostream>
using namespace std;
int main()
{
std::string str[] = ""Mymommaalwayssaid,\"Lifewaslikeaboxofchocolates.Youneverknowwhatyou'regonnaget.""
int n ,i;
string UP[str.size()];
for(int i=0;i<=str.size();i++)
{
if(int(str[i])<=90 && int(str[i])>=65)
{ n = int(str[i]);
n= n+32;
UP[i]=char(n);
}
else
UP[i] = str[i];
}
cout<<UP<<endl;
return 0;
}
A definition close to your code and correcting the errors is :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Mymommaalwayssaid,\"Lifewaslikeaboxofchocolates.Youneverknowwhatyou'regonnaget.";
string lower = str;
for (size_t i = 0; i < lower.length(); ++i) // can also use an iterator
{
char c = lower[i];
if ((c >= 'A') && (c <= 'Z'))
lower[i] += ('a' - 'A');
}
cout << lower <<endl;
return 0;
}
Compilation and execution
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra t.cc
pi@raspberrypi:/tmp $ ./a.out
mymommaalwayssaid,"lifewaslikeaboxofchocolates.youneverknowwhatyou'regonnaget.
But I do not understand because you say without using STL and you use std::string .
Without std::string ( iostream can be replaced by stdio.h )
#include <iostream>
using namespace std;
int main()
{
const char str[] = "Mymommaalwayssaid,\"Lifewaslikeaboxofchocolates.Youneverknowwhatyou'regonnaget.";
char lower[sizeof(str) + 1]; // if str content unknown so sizeof unknown => strlen and new/malloc etc
for (size_t i = 0 ; i != sizeof(str); ++i)
{
char c = str[i];
if ((c >= 'A') && (c <= 'Z'))
c += ('a' - 'A');
lower[i] = c;
}
lower[sizeof(str)] = 0;
cout << lower <<endl;
return 0;
}
Compilation and execution
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra t.cc
pi@raspberrypi:/tmp $ ./a.out
mymommaalwayssaid,"lifewaslikeaboxofchocolates.youneverknowwhatyou'regonnaget.
Well, the question has several confusing points. First, the question asks for an example of tolower(), but the code seems to want to create a string called UP.
I went ahead and created a string_to_lower() function. I did not want to use the same name as tolower(char* str), though that would have worked.
#include <iostream>
#include <string>
std::string string_to_lower(std::string str) {
const uint diff = 'a' - 'A'; // create a constant from the ascii chars
std::string lower; // return string. Could be done with an inline reference.
lower.reserve(str.length());
for (int i = 0; i < str.size(); i++) { // Use a ternary to set each char
lower += (str[i] >= 'A' && str[i] <= 'Z') ? str[i] + diff : str[i];
}
return lower;
}
int main()
{
std::string str = {"My momma always said, \"Life "
"is like a box of chocolates."
"You never know what you're gonna get.\""};
std::string lower = string_to_lower(str);
std::cout << lower << std::endl;
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.