0

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?

Dreadlock
  • 35
  • 1
  • 5
  • 6
    Use `std::replace`. – chris Mar 30 '14 at 20:23
  • The problem is badly worded: the description says "pad to 15 characters", but the title is "replace spaces with underscores". – Mark Mar 30 '14 at 20:32
  • Thanks Chris, I didn't know there was a replace function I could've used so that takes care of replacing empty spaces. However, how do I append a certain number of empty spaces if the username is less than 15 characters long? – Dreadlock Mar 30 '14 at 20:32
  • There's an `append` overload that takes a character and how many. – chris Mar 30 '14 at 20:34

4 Answers4

2

The string::string(char *) constructor expects a null-terminated string. arr is not null-terminated, so the constructor walks off the end of the array and keeps appending random garbage from memory until it finds a byte with the value '0'.

If you want to use this method to pad the string with underscores, you should make arr one character larger than the number of underscores needed, and set that last spot to the value '\0'.

Mark
  • 2,792
  • 2
  • 18
  • 31
0

A better way is to use one of the append functions std::basic_string::append.

The std::string::push_back can be used inside a loop to add underscores until the length is 15 (or whatever limit has to be enforced).

Similarly, use the std::string::replace function to replace all user entered spaces with an underscore. The

string spaces (int i)

function is no longer needed (solving the non-terminated array problem Mark has listed).

0

I was able to solve it by using the following:

string addUnderscore(string text, int size){
   text = text.append(size, ' ');
   for(int i = 0; i < text.length(); i++)
   {
      if(text[i] == ' ')
      text[i] = '_';
   }
   return text;
}

Thank you.

Dreadlock
  • 35
  • 1
  • 5
0

The task can be done in two steps. At first you should replace all spaces with undescores in the original string. For example

std::replace( str.begin(), str.end(), ' ', '_' );

And after that you should check whether the size of the string is less than 15 and if so then append the string with underscores

if ( str.size() < 15 ) str.resize( 15, '_' );

So combining these two statements you will get

#include <algorithm>
#include <string>

//...

std::replace( str.begin(), str.end(), ' ', '_' );
if ( str.size() < 15 ) str.resize( 15, '_' );

As for your function then it is wrong. First of all it is not C++ compliant. In C++ there is no variable length arrays. So you may not write

string spaces(int i){
        char arr[i];
        //...
}

Also array arr is not null terminated. So call of constructor string space(arr); has undefined behaviour.

The function could look the following way

void format( std::string &str )
{
    std::replace( str.begin(), str.end(), ' ', '_' );
    if ( str.size() < 15 ) str.resize( 15, '_' );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335