Need Programming Help

soulslicer

Tank
Joined
Mar 16, 2007
Messages
4,623
Reaction score
12
Okay, my Objective is to say have an input into a certain variable, say like the word "HELLO", intro a string,

and..I need to seperate out the CHARACTERS in "HELLO", say like "H", "E", "L", "L", "O"

and have them stored in a char[] array,

such that that char[] character must be = {'H', 'E', 'L', 'L', 'O'}

How do i possibly do this????

I've tried:
Code:
Console.WriteLine("Enter the letters you want removed");
            string remover = Console.ReadLine();
            char[] remover1;
            int counter = 0;
            foreach (char character in remover)
            {

                Console.WriteLine(character);
NOW I ave seperated the characters, now what do i DO??
                
            }
            Console.ReadLine();
 
That looks like .NET to me which I have no experiance in. But instead of doing a foreach most programming languages should have a function that will make this array for you. It will probably be under split(). In PHP for example its str_split.
 
What language are you using? That looks like some .NET there.

Here's how I would do something like this in C#. If this is what you're asking. At first I thought you were asking how to go about removing specific letters from a string. So I wrote something up, but realized that's not what you were asking.

If you're using a .NET language you should have a very handy ToCharArray() method for the string class that will let you quickly split a string into a character array. If you're not looking for this easy method, please let me know.

Code:
        static void Main(string[] args)
        {

            Console.WriteLine("Please type something!");

            string sentence = Console.ReadLine();

            char[] wordSplit = sentence.ToCharArray();

            Console.WriteLine("\nDisplaying what you entered now!\n");

            foreach (char character in wordSplit)
            {
                Console.WriteLine(character);
            }


            Console.ReadLine();
        }

epfwc2.jpg




If you're trying to do it without the built in method, you can always do it on your own in a way like this.

Code:
 static void Main(string[] args)
        {

            Console.WriteLine("Please type something!");

            string sentence = Console.ReadLine();

            char[] wordSplit = new char[sentence.Length];

            for (int i = 0; i < sentence.Length; i++)
            {
                wordSplit[i] = (char) sentence[i];
            }

            Console.WriteLine("\nDisplaying what you wrote!\n");

            foreach (char character in wordSplit)
            {
                Console.WriteLine(character);
            }

            Console.ReadLine();
        }

The result is exactly the same.


Does this help soulslicer?
 
The other answers are fine and ToCharArray() is indeed the method you want. But if you're trying to remove characters from a string (which I'm gathering from the code you posted) then it's generally better to select the characters you do want rather than removing the ones you don't want (selecting will result in the creation of one new string, removal will result in the creation of a string for every character that you remove).

Some code to do just that:

Code:
      string input = Console.ReadLine();
      string charsToRemove = Console.ReadLine();

      string newString = new string((from c in input
                                     where !charsToRemove.Contains(c)
                                     select c).ToArray());

      Console.WriteLine("The new string: " + newString);

This simply generates a string from all the characters in the input that are not part of the string of characters that you want to remove.
 
Man, I need to pick up a LINQ book again.
 
Awesome, I got it, using razz's method, thx alot guys. awesome
 
Awesome, I got it, using razz's method, thx alot guys. awesome

Let it be known that you can also do it all in one line, like this.

Code:
            char[] sentence = Console.ReadLine().ToCharArray();
 
Back
Top