Thread: How to insert spaces in string?

  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,868
    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-11-2012 at 12:17 AM.
    "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,318
    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,868
    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
    "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-04-2010, 12:04 AM
  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, 09:15 AM