Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I am on a work committee for activities and have been give the task of making some Anagrams. I have made and app that takes a string loops out the spaces and makes AN anagram. Concurrently, the app made note of the point at which each character the space was taken out. I now need to loop back in the spaces into a list or an array at the point of the space? So I have it to work with some manual alteration, so I have completed my task but not to the standard, of the programme doing the work.

Example

The Queen of the Damned

array space position (5,11,14,18).

I ten have in a List an array

something like this

'eaeudhftnnmeedetqoh' and I wish to put a space in at point 5,11,14 and 18?

I have tried a 2d array and it works for the first space, but none after. I could hard code based on maybe 10 spaces but that would be sufficient for 99.9999999% of all situations, but it did break at that .0000001% times!
Posted
Comments
BillWoodruff 27-Oct-15 10:47am    
If you scramble the letters for the anagram, what is the point of inserting spaces at the same place in the scrambled letters they occur in the "plain text" ... to give a clue to the length of the plain text, and the length of each word within it ?
MarcusCole6833 10-Nov-15 12:50pm    
cheers for your code here is how I used at least part of it

http://www.codeproject.com/Articles/1053368/Using-Lists-to-make-an-Anagram-Application

As to advice in the solution 1 by Foothill[^]...
C#
string s = "eaeudhftnnmeedetqoh";
int[] spaces = new int[]{5,11,14,18};
StringBuilder sb = new StringBuilder();
int j = 0;
for(int i=0; i<s.Length; i++)
{
    if (i == spaces[j])
    {
        sb.Append(" " + s[i]) ;
        j+=1;
    }
    else
    {
        sb.Append(s[i]);
    }
}
Console.WriteLine(sb.ToString());
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Oct-15 10:35am    
5ed.
—SA
Maciej Los 27-Oct-15 11:13am    
Thank you, Sergey.
BillWoodruff 27-Oct-15 12:09pm    
Hi Maciej, Are you aware that the output of your code is:

eaeud hftnnm eed etqo h

I'm not saying that's wrong :) I think that what the OP actually wants here is not absolutely clear.

My guess is they want the scrambled string broken into chunks whose size match the size of the words in the original string. I've asked them to clarify this.

cheers, Bill
Maciej Los 27-Oct-15 12:14pm    
I didn't test it. Even if my code is wrong, this is an area where OP can change it to his needs. Thank you, Bill.
BillWoodruff 27-Oct-15 12:22pm    
I do not believe this statement reflects the intelligence, values, and ability, of one of CP QA's most helpful, and prolific, contributors ... you !
Assuming you are satisfied with the randomized string created by your code (are you using Fisher-Yates ?), then:
C#
// requires Linq
public string InsertSpaces(string source, params int[] insertats)
{
    // make sure inserts are in ascending order
    var sortedparams = insertats.ToList<int>().OrderBy(i => i);
    
    StringBuilder sb = new StringBuilder(source);

    foreach (var i in sortedparams) sb.Insert(i - 2, space);

    return sb.ToString();
}

// sample usage:

public const char space = ' ';

string anagram = "eaeudhftnnmeedetqoh"

string anagramwithspaces = InsertSpaces(anagram, 5,11,14,18);
 
// result: eae udhft nn mee detqoh
 
Share this answer
 
v3
Comments
Maciej Los 27-Oct-15 12:14pm    
5ed!
BillWoodruff 27-Oct-15 12:24pm    
Hi Maciej, thanks for the up-vote !

I don't have your e-mail, so I'll stick this here.

I'd appreciate it if you leave a brief comment on any post of mine you edit that lets me know what you changed; I have been looking at the versions of the post, but your edit (version 2) is not present. Since I test any code I post, I'm always curious to see if there's some aspect of the CP code-editor's little bugs that I haven't yet become aware of.

Or, if it's not too much trouble, you could bold what you changed ?

I value your contributions, and welcome your (and anyone else who has your intelligence) edits !

thanks, Bill
Maciej Los 27-Oct-15 15:28pm    
You're very welcome. It's hard to "bold" changes. Imagine, when you type (or paste) a statement like: List<int>, an editor (input window, control) adds extra </int> at the end of code block. Seems, that input window tries to parse html tags. I'll promise to explain next time what i had to change.
You can try using the StringBuilder class instead of an arry.
 
Share this answer
 
Comments
Maciej Los 27-Oct-15 11:28am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900