Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Inserting Spaces in a String (Java)
New Posts  All Forums:Forum Nav:

Inserting Spaces in a String (Java)

post #1 of 8
Thread Starter 
Hi everyone,

I'm trying to take a string:
Code:
ABCDE

and add in spaces such that:
Code:
A BCDE
A B CDE
A B C DE
A B C D E
AB C D E
AB CD E
AB CDE
AB C DE
ABC DE
ABCD E

I am having trouble trying to figure out how to do this recursively, so any help would be greatly appreciated. I can generate a string that looks like:
Code:
A B C D E

but I am having trouble trying to figure out how to do this.

Edit: Post 1000 grouphug.gif
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
post #2 of 8
Do you want it to place a random space in the string?

That would be a for loop with a random num gen.

for(i=0 i
ran num x
insert space at location x
}
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
post #3 of 8
Maybe split the string into a char array, then write the recursive function to add the spaces in.
Containment
(18 items)
 
Metamorphosis
(19 items)
 
 
CPUMotherboardGraphicsRAM
AMD 955BE Gigabyte GA-770TA-UD3 Asus GTX560ti 12GB DDR3 1333 CL7 
Hard DriveHard DriveOptical DriveCooling
Spinpoint F3 750GB Seagate 1TB None Custom loop 
OSMonitorMonitorKeyboard
W7 Ultimate x64 Asus ML249H Asus VE228H Alienware TactX 
PowerCaseMouseMouse Pad
OCZ 550W Antec 902 Alienware TactX Razer Sphex 
AudioAudio
E-MU 0404 USB Ultrasone DJ1 Pro Headphones 
CPUCPUMotherboardGraphics
Xeon E5405 Xeon E5405 SuperMicro X7DA3 ATI X1800 
RAMHard DriveCoolingCooling
16GB DDR2 FB-DIMM Various Hyper 101 Hyper 101 
OSMonitorPowerCase
ESXi 5.0 Headless Antec NEO ECO 620W Rosewill RSV-L4000 
MouseMouse PadOtherOther
None None HP P400 IPMI Card 
Other
HP Dual Gigabit NIC 
  hide details  
Reply
Containment
(18 items)
 
Metamorphosis
(19 items)
 
 
CPUMotherboardGraphicsRAM
AMD 955BE Gigabyte GA-770TA-UD3 Asus GTX560ti 12GB DDR3 1333 CL7 
Hard DriveHard DriveOptical DriveCooling
Spinpoint F3 750GB Seagate 1TB None Custom loop 
OSMonitorMonitorKeyboard
W7 Ultimate x64 Asus ML249H Asus VE228H Alienware TactX 
PowerCaseMouseMouse Pad
OCZ 550W Antec 902 Alienware TactX Razer Sphex 
AudioAudio
E-MU 0404 USB Ultrasone DJ1 Pro Headphones 
CPUCPUMotherboardGraphics
Xeon E5405 Xeon E5405 SuperMicro X7DA3 ATI X1800 
RAMHard DriveCoolingCooling
16GB DDR2 FB-DIMM Various Hyper 101 Hyper 101 
OSMonitorPowerCase
ESXi 5.0 Headless Antec NEO ECO 620W Rosewill RSV-L4000 
MouseMouse PadOtherOther
None None HP P400 IPMI Card 
Other
HP Dual Gigabit NIC 
  hide details  
Reply
post #4 of 8
Written in C#, probably close to how you need it
Code:

static String putSpaces(string needsSpaces)
        {
            if (needsSpaces.Length <= 1)
                return needsSpaces;



            return needsSpaces.Substring(0, 1) + " " +                  
                          putSpaces(needsSpaces.Substring(1, needsSpaces.Length - 1));

        }



Pretty much if its only one letter long or less you return the string because you need no spaces, otherwise you concatenate the first letter, a space after and then you add the string that is returned from sending it back into the function. When it gets to the last letter it wont add a space after because the length will be less than 1 so it just returns the last letter.
Edited by mykah89 - 3/16/11 at 8:45am
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #5 of 8
Thread Starter 
The problem is, I also need it to have the other prefixes, such as:
Code:
AB C D E
AB CD E

or

ABC DE
ABC D E


Essentially, the function must put a space in every possible position, or in simpler terms, put them in every possible combination. The segmentation of a string s is equal to a set containing s itself, and a set union of each substring X of s with the segmentation of s\X.

Here is one way to do it using a hash set (which would be incorrect seeing as how I don't know about hash sets yet):
Code:
public static Set<Set<String>> segment(String s) {
    // `s` itself.
    Set<Set<String>> result = new LinkedHashSet<Set<String>>();
    Set<String> root = new LinkedHashSet<String>();
    root.add(s);
    result.add(root);   

    // set union of each substring `X` (prefix) of `s` with `s\X` (rest).
    for (int i = 1; i < s.length(); i++) {   
        String prefix = s.substring(0, i);
        String rest = s.substring(i);
        for (Set<String> segments : segment(rest)) {
            Set<String> segment = new LinkedHashSet<String>();
            segment.add(prefix);
            segment.addAll(segments);
            result.add(segment);
        }
    }
    return result;
}


Anyone got any more insight, because other than using a HashSet (which I kinda found somewhere else) I have no clue how to do this.

PS. In fact, I have no clue how this method works even using the HashSet. Esentially I need a HashSet-Less solution.

THANKS EVERYONE biggrin.gif

+rep to all the help so far
Edited by CovertCover - 3/22/11 at 2:54pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
post #6 of 8
A recursive function could probably be done the prettiest. But easily you could make the string a char array(theres a function for this in java), take each of the characters out and put it into an arraylist, and start inserting all possible combos of spaces.
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
post #7 of 8
all possible combinations = all permutations.

Try searching for that and maybe you'll find better websites.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #8 of 8
i remember this problem from a computer science 2 lab. i could look up my solution if you still need help.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Inserting Spaces in a String (Java)