Problem with string pop_back() function
There is an error with the pop_back function in this case.
In this code finalpuncuation(string) is a bool function.
1 2 3 4 5 6 7
|
void dePuncuation(const std::string &word) {
char c = word[0];
if (('A' <= c) && (c <= 'Z'))
c += ('a' - 'A');
if (finalpuncuation(word))
word.pop_back();
}
|
Can you explain what const
means? Also the effect of operator += on a char, and the value you pass to it?
Compilation issues aside, something along these lines may be more appropriate:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
std::string lowered(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](char c) { return std::tolower(c); });
return s;
}
std::string unpunctuated(const std::string& s)
{
std::string result;
std::copy_if(s.begin(), s.end(), std::back_inserter<std::string>(result),
[](char c) { return !std::ispunct(c); });
return result;
}
// for convenience
std::string reformatted(const std::string& s) { return lowered(unpunctuated(s)); }
int main()
{
std::string punctuated = "\"This is a (some-what) punctuated string!?!.**@#$\"";
std::cout << "Original: " << punctuated << '\n';
std::cout << "Modified: " << reformatted(punctuated) << '\n';
}
|
where each individual function has a clear job to do and each can be used independently of the other if so desired.
Topic archived. No new replies allowed.