So I have an input string of characters. If the string contain spaces or is less than 15 characters long I have to replace the empty spaces with an underscore("_").
This is my code:
#include <string>
#include <iostream>
using namespace std;
string spaces(int i){
char arr[i];
for(int j=0; j<i; j++){
arr[j]='_';
}
string space(arr);
return space;
}
int main(){
string str = "USERNAME12034";
if(str.size() < 15){
// get size of username;
int size = 15-str.size();
str = str.append(spaces(size));
}
cout << str << endl;
return 0;
}
When I compile and run it, this is the output:
USERNAME12034__�O�
How do I get rid of those characters at the end?