So what you want is a function that creates a new string which is a copy of the string being past... Why not change the 'const std::string& a_string' parameter to 'std::string& a_string' and change original string or is that outside the requirements?
Your question, get the iterators for the string's begin and end and then increment the begin value by one.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
I didn't take the time and read your post completely..Ooops
Try something like below.
#include <iostream>
#include <string>
#include <algorithm>
#include <locale>
std::string lower_string (const std::string& a_string, const std::locale& a_locale )
{
char *str = new char(a_string.size());
std::string::const_iterator begin = a_string.begin();
std::string::const_iterator end = a_string.end();
copy(begin, end, str);
std::string ans;
std::use_facet< std::ctype<char> > ( a_locale ).tolower ( str + 1, str + a_string.size() );
ans = str;
return ans;
}
int main(int argc, char *argv[])
{
std::locale loc("");
std::string name("A nAME tO PASS aLONG");
std::string ans = lower_string(name, loc);
std::cout << ans << std::endl;
return 0;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Here's a tighter version.
#include <iostream>
#include <string>
#include <algorithm>
#include <locale>
std::string lower_string (const std::string& a_string, const std::locale& a_locale )
{
char *str = new char[a_string.size() + 1];
std::string::const_iterator begin = a_string.begin();
std::string::const_iterator end = a_string.end();
copy(begin, end, str);
std::use_facet< std::ctype<char> > ( a_locale ).tolower ( str + 1, str + a_string.size() );
std::string ans(str);
delete [] str;
return ans;
}
int main(int argc, char *argv[])
{
std::locale loc("");
std::string name("A nAME tO PASS aLONG");
std::string ans = lower_string(name, loc);
std::cout << ans << std::endl;
return 0;
}
Its been a while since I look at C++ code.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387