-1

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)
        {
            FirstPart();
        }
        #endregion



        #region
        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());
            Console.WriteLine(PItoArray);

End of where my question lies.

        }
        #endregion

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

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

        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);

    ConsoleWriteLine(Var);

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.
rlandster
  • 7,294
  • 14
  • 58
  • 96

3 Answers3

0

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).

potibas
  • 619
  • 5
  • 7
0

Your Max can be no bigger than pi.Length as this is the string length. First argument in pi.ToCharArray - Min - is starting index of the string.You can choose Min to be anything from 0 to Max. The second argument in pi.ToCharArray should be the length of the charArray you want, which can be anything from 0 to Max - Min. You can read this documentation also:String.ToCharArray(int32 start, int32 length)

αNerd
  • 528
  • 1
  • 6
  • 11
0

Hello Again and thanks for the quick answers guys i did it a little differently (Although with my luck one of you two said this exactly.)here's What I came up with:

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

namespace SearchCharInString
{
    class Program
    {



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



        #region // HERE IS WHERE CHANGED HAPPENED!!
        static void FirstPart()
        {

            Console.WriteLine(PItoArray());
        }
        #endregion



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

            return S;
        }
        #endregion

This Is What I Changed

        #region // HERE IS WHERE CHANGED HAPPENED!!
        static char[]PItoArray() 
            {
            string PI = PlayerInput();

            int piLength = PI.Length;

            return PI.ToCharArray(0,piLength);

            }
        #endregion



    }
}

This is how I call it

#region // HERE IS WHERE CHANGED HAPPENED!!
    static void FirstPart()
    {

        Console.WriteLine(PItoArray());
    }
    #endregion

I will be recognizing your answers though because it helps me with research. Thanks again guys. I believe Now I should be able to search my chars for certain things maybe even get it to make out Certain words from if statements using alphabet index to this code.