-1

I found a nice code to data as anagram of a string but when I am displaying, I am getting in continuous string but I want with space after 3 char.

static void Main(string[] args)
{
    string result = "";
    var res = "abc".Anagrams();
    foreach(var anagram in res)
    { 
        if(anagram.Count() == 3)
        {
            Console.Write(anagram.MergeToStr());
        }
    }
}

public static string MergeToStr(this IEnumerable<char> chars)
{
    return new string (chars.ToArray());
}

My OutPut

abcacbbacbcacabcba

But My expected output should be

abc acb bac bca cab cba

How can I add spaces to my output?

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
vish1990
  • 272
  • 3
  • 16
  • 1
    Well you don't have anything to add a space - what happens if you just add `Console.Write(" ");` after your current `Console.Write` method? And why do you need to check the count of `anagram`? – Jon Skeet May 28 '18 at 17:31
  • 1
    @DaisyShipton guessing [this is the implementation](https://stackoverflow.com/a/4410723/1541563) for `Anagrams()`, in which case the length check would be necessary. – Patrick Roberts May 28 '18 at 17:33
  • because if I do not count ,I am getting array of anagram with all char.Eg. a ,b,c,ab etc. and I need only permutation of abc(eg abc,bca etc) – vish1990 May 28 '18 at 17:34
  • @DaisyShipton-yes,i am trying to implement the same with my modification – vish1990 May 28 '18 at 17:36
  • That seems like a poor implementation of a method called `Anagrams`, IMO. "a" isn't an anagram of "abc"... Ah well. – Jon Skeet May 28 '18 at 18:01

2 Answers2

3

You do not print any white spaces between your anagrams.

 foreach(var anagram in res)
 { 
     if(anagram.Count() != 3) continue;

     Console.Write($"{anagram.MergeToStr()} ");
 }

If you do not want to use string interpolation, you can simply:

Console.Write(anagram.MergeToStr() + " ");

EDIT: Ordering the response:

var res = "abc".Anagrams().Where(x => x.Count() == 3).Select(x => x.MergeToStr()).OrderBy(x => x).ToList();
foreach(var word in res)
{
    Console.Write($"{word} ");
}
  • thanks but Now I am not getting string value in alphabeticalal order.Can you suggest something for that.i am getting[dog dgo odg ogd gdo god ] when I should get [dgo dog gdo god odg ogd] – vish1990 May 28 '18 at 18:26
  • I have edited the response. Make sure to import the Linq namespace. (using System.Linq;). Does this help you? – Florin-Constantin Ciubotariu May 28 '18 at 18:33
2

Below example uses Linq and string.Join for the required output.

static void Main(string[] args)
{
    string result = "";
    var res = "abc".Anagrams();
    Console.WriteLine(string.Join("   ", res.Where(a => a.Count() == 3).Select(a => a.MergeToStr()).TrimRight());
}

This would produce output as abc acb bac bca cab cba

user1672994
  • 10,509
  • 1
  • 19
  • 32
  • Thanks but it will also give non alphabetical .I am getting [dog dgo odg ogd gdo god] whereas I am expecting [dgo dog gdo god odg ogd] .any suggestion? – vish1990 May 28 '18 at 18:38