1

When I try to copy arrays into a jagged array. My goal is to take an array of type char, separate the "words" into separate arrays (I use an already working function for it) and want to put them into an array.

static char[][] split_string(char[] str)
{
    int length = count_words(str);
    char[][] sentence = new char[length][];
    int start = 0;
    int end = 0;
    int word = 0;
    int i = -1;
    while (i != str.Length)
    {
     i++;
     if (str[i]==' ')
     {
      end = i-1;
      char[] aux_array = substring(str, start, end);
      //issue
      aux_array.CopyTo(sentence[word], 0);
      //alternative (not working either)
      /*
      for(int j=0; j<aux_array.Length;j++)
      {
       sentence[word][j] = aux_array[j];
      }
      */
      while (str[i]==' ')
      {
       i++;
      }
      word++;
      start = i;
     }
    }
    return sentence;
   }

For information,
substring if of the form: substring(array, int, int) -> array count_word is of the form: count_word(array) -> int

2
  • Hi Bazin, welcome to SO. To help others help you, please mention what issue(s) you are facing with your code? What did you expect, and what your code is doing actually? Please read How to Ask and minimal reproducible example. Commented Dec 11, 2015 at 9:51
  • Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not"! Commented Dec 11, 2015 at 9:56

2 Answers 2

2

My goal is to take an array of type char, separate the "words" into separate arrays (I use an already working function for it) and want to put them into an array.

Then just put them into array

//...
sentence[word] = substring(str, start, end);

Note that the jagged array elements are null by default and you didn't allocate them, so you probably are getting null reference exception. If you really need to do a copy of the returned array, then the easiest way is to use Array.Clone method like this

sentence[word] = (char[])substring(str, start, end).Clone();
Sign up to request clarification or add additional context in comments.

2 Comments

apparently it's working, but now i get an "out of range exception" at "if (str[i]==' ')" after i try to run it
@LoïcBazin Well, that's another problem in your code which is unrelated to the original question. You can fix it by using while (i < str.Length && str[i]==' ') But it looks like your code will also miss the last word.
0

It is easier to work with strings and not raw char arrays but I assume it is with intention that you have decided to use char arrays.

One way to simplify your code is to build the char array as you go instead of preallocating it. .NET arrays have fixed size but List<T> allows you to grow a collection of items.

You can also change your function into an iterator block to simplify it further. When a word is complete you yield return it to the caller.

IEnumerable<char[]> SplitString(char[] str) {
  var word = new List<char>();
  foreach (var ch in str) {
    if (ch == ' ') {
      if (word.Count > 0) {
        yield return word.ToArray();
        word = new List<char>();
      }
    }
    else
      word.Add(ch);
  }
  if (word.Count > 0)
    yield return word.ToArray();
}

This function will not return an array so if you want an array of arrays (jagged array) you need to use ToArray():

var str = "The quick brown fox jumps over the lazy dog".ToCharArray();
var result = SplitString(str).ToArray();

This code will correctly handle multiple spaces and spaces in the beginning and end of the source string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.