Question

I am still a beginner in C# and I know there is a method that can be used to do this but I can't seem to find it online.

I have a function that permutates a word

static void Main(string[] args)
{
    string[] list = "a b c d".Split();
    foreach (string[] permutation in Permutations<string>.AllFor(list))
    {
        System.Console.WriteLine(string.Join(" ", permutation));
    }
}

However it only works with words that are broken up. (eg. "a b c d" ) Since that is not really a practical way to ask a user for input, I want to find a way to take a word from the user (an unbroken word like "hello" ) and break it up for the function to understand. Eg. form the input word of the use "happy" to a spaced word for the program to understand = "h a p p y"

I tried this code:

//splits the word into an array
string[] arr = name.Split();

//splits the array with spaces to enter into the program
name = string.Join(" ",arr);

arr = name.Split();

But it just ends up coming out unbroken anyway. Can someone tell me the easiest way to do this?

Just to mention I am still a beginner in C# and programming in total I might not understand some of the higher level concepts. I have been through some answers on this website and I have seen some answers that I don't understand at all.

Was it helpful?

Solution 3

    char[] array=input.ToArray();
string val="";
for(int i=0;i<array.Length;i++)
{
val+=array[i]+" ";
}

this will give you a string with spaces like you wanted Val create an array with the string length

string[] strarray=new string[val.Length];


   for(int i=0;i<strarray.Length;i++)
{
strarray[i]=val.Substring(i,len); //**i** is for string index,,,**len** string length in each index
}

OTHER TIPS

You can loop over the string to convert it to an array, and then use Join.

using System.Text.RegularExpressions;
using System;

public class Program{
    public static void Main(string[] args) {
        string v = "hello";

        // Convert into the a string array, the old-fashioned way.
        string[] name = new string[v.Length];
        for (int i = 0; i < v.Length; i++)
        name[i] = v[i] + ""; 


        string feedToPermutationFunction = string.Join(" ",name));
        // Feed the above string into your permutation code.
    }
}

You just need to separate each character and then concatenate them with a space:

This is the simplest way:

var userInput = Console.ReadLine();

var output = string.Join<char>(" ", userInput);

Console.WriteLine(output);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top