Like Tree1Likes
  • 1 Post By novacain

How to insert spaces in string?

This is a discussion on How to insert spaces in string? within the C++ Programming forums, part of the General Programming Boards category; I am trying to insert spaces in string for example: string = "hellohowarejames" i woud like to insert a space ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    How to insert spaces in string?

    I am trying to insert spaces in string

    for example:

    string = "hellohowarejames"

    i woud like to insert a space for every 4 letters

    so it will look like

    "hell ohow arej ames"

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Something like this untested, bug filled code?

    Code:
    string AddSpaces(string Input)
    {
    	int length = Input.size();
    	string output = string.empty;
    
    	//for each block of 4 characters
    	for(int i =0 ; i< length; i=i+4)
    	{
    		//copy 4 characters and a space to the output
    		output += Input.substr(i, 4) + " ";
    
    	}
    	return output;
    }
    Last edited by novacain; 12-10-2012 at 11:17 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by novacain View Post
    Code:
    	string output = string.empty;
    He he, Spot the C# programmer!
    In C++ we just declare it and leave the default constructor to make it empty.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Quote Originally Posted by iMalc View Post
    He he, Spot the C# programmer!
    In C++ we just declare it and leave the default constructor to make it empty.
    You are lucky I did not write
    Code:
    output := '';
    I am coding everyday in PL/SQL, C#, VB.NET and ASP.NET at the moment, with some JavaScript thrown in to be annoying.

    I keep mixing syntax up and adding 'THEN' after loops/IFs.

    Worse is when I code in PL/SQL
    Code:
    IF (someVar <> NULL) THEN
    iMalc likes this.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-03-2010, 11:04 PM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. Replies: 8
    Last Post: 10-17-2005, 07:01 PM
  4. Insert int into string
    By lpmrhahn in forum C Programming
    Replies: 1
    Last Post: 04-09-2004, 02:10 PM
  5. Unable to insert a string into another string
    By Jacquiline in forum C Programming
    Replies: 0
    Last Post: 04-05-2003, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21