I cant seem to return str
in my RemSpacePunct
function.
I wanted to make a function that removes spaces in a string but when I run the code, it won't remove the space.
string RemSpacePunct(string str);
string sort(string str);
int main(){
string s = "partial men";
sort(s.begin(), s.end());
RemSpacePunct(s);
cout << s;
}
string RemSpacePunct(string str)
{
//add code here
int s, i;
s = str.length();
for(i = 0; i < s; i++){
if(isalnum(str[i]) == false){
str.erase(i, 1);
}
}
return str;
}
You're ignoring the return value. str
is copied when it's passed to the function, so any modifications you perform on the argument aren't reflected back to the original value. Instead, you should return a modified value - which you do, but you then ignore it instead of using it:
string result = RemSpacePunct(s);
cout << result;
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.