Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:37:35+00:00 2026-05-20T12:37:35+00:00

I am trying to write something that will replace all the spaces in a

  • 0

I am trying to write something that will replace all the spaces in a string with an underscore.

What I have so far.

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

For the most part this would work, if I was doing something like.

string word = "hello stackoverflow";
word = space2underscore(word);
cout << word;

That would output “hello_stackoverflow”, which is just what I want.

However if I was to do something like

string word;
cin >> word;
word = space2underscore(word);
cout << word;

I would just get the first word, “hello”.

Does anybody know a fix for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:37:36+00:00Added an answer on May 20, 2026 at 12:37 pm

    The problem is that cin >> word is only going to read in the first word. If you want to operate on a whole like at a time, you should use std::getline.

    For example:

    std::string s;
    std::getline(std::cin, s);
    s = space2underscore(s);
    std::cout << s << std::endl;
    

    Also, you may want to check that you actually were able to read a line. You can do that like this:

    std::string s;
    if(std::getline(std::cin, s)) {
        s = space2underscore(s);
        std::cout << s << std::endl;
    }
    

    Finally, as a side note, you could probably write your function in a cleaner way. Personally I would write it like this:

    std::string space2underscore(std::string text) {
        for(std::string::iterator it = text.begin(); it != text.end(); ++it) {
            if(*it == ' ') {
                *it = '_';
            }
        }
        return text;
    }
    

    Or for bonus points, use std::transform!

    EDIT:
    If you happen to be lucky enough to be able to use c++0x features (and I know that’s a big if) you could use lambdas and std::transform, which results in some very simple code:

    std::string s = "hello stackoverflow";
    std::transform(s.begin(), s.end(), s.begin(), [](char ch) {
        return ch == ' ' ? '_' : ch;
    });
    std::cout << s << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a regex function that will identify and replace a single
I'm trying to write something that will allow a user to use a keyboard/mouse
I'm trying to write something that puts the contents of the message on a
I am trying to write my first real python function that does something real.
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
I'm trying to write a stored procedure that will create a new FILEGROUP based
I'm trying to write a program in Scala, that will accept SOAP-requests, get the
I am trying to write a function that will check if an object exists:
I am trying to write a Java application that will query the WMI on
I'm trying to write something to simplify experimentation with SVG path, but i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.