HiradorTheDeathLord HiradorTheDeathLord - 1 year ago 73
C# Question

How to establish a min max value, from a char array to same char array for output

Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
class Program

#region//Main() Put everything thats finished in here to output it to Console Apllication.
static void Main(string[] args)

static void FirstPart() // don't know what else to call this.
//Console.WriteLine(numS());// commented out

This is where my question lies.

string pi = Console.ReadLine();
PItoArray = pi.ToCharArray(pi.Min(),pi.Max());

End of where my question lies.


#region //numS() gets number of char in player input.
//static int numS()
// int num = PlayerInput().Length;
// return num;

#region//PlayerInput() Gets whatever player writes in console apllication
static string PlayerInput()
string S = Console.ReadLine();
return S;

static char[] PItoArray;


Now these are the details (If you need more please feel free to reply as such.):

i tried to use num.max instead of pi.max, my personal preference to split everything up, but with both cases I get an out of range exception where the index is either negative or greater than the collection
the way i'm thinking it should work is that it

  1. sets whatever i write, to pi variable

  2. then splits each char into char array

  3. then uses pi to determine what is min and max.

  4. then it should write it out exactly the way it was written. ex. ""hello" input Turns into "hello" output.

Now Here's the 3 questions I have:

  1. Is my logic correct? If not please excuse me i'm 18 and mainly i like to explore what i can and can't do.

  2. How do I use char array's min and max value ex. If i write Hello i can use

    char[] Var = pi.ToCharArray(0,5);


The output would be "Hello" Right? but what if i wrote "Hello World" how would i get all of the char's in a string no matter the amount of char's in said string, or a better way of asking is how do i use the amount of char's in a string to get a min & max value to the ToCharArray(min,max) so if I wrote a 10 letter sentence or a 100 letter sentence I would never get a out of range exception?

  1. Is there a way to do this in a simple 1-5 line code? I'm not lazy but easier is easier so why not use it.

Answer Source

If you want the string converted to a char array of exactly the same size, you can just call pi.ToCharArray() without any argument, and it will automatically create a char array containing the exact same number of chars in the original string.

Also please note that what calling pi.Max() will give you back is the char with the highest char code according to the ASCII table, not the string size, as it appears you are assuming. ("Hello, world".Max() returns the char 'r' which has the code 114 - the highest char code inside the string).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download