2

I want to split a string into an array of words without using string.Split. I tried already this code and it is working but cant assign the result into the array

string str = "Hello, how are you?";
string tmp = "";
int word_counter = 0;
for (int i = 0; i < str.Length; i++)
{
     if (str[i] == ' ')
     {
         word_counter++;
     }
}
string[] words = new string[word_counter+1];

for (int i = 0; i < str.Length; i++)
{
    if (str[i] != ' ')
    {
        tmp = tmp + str[i];
        continue;
    }
    // here is the problem, i cant assign every tmp in the array
    for (int j = 0; j < words.Length; j++)
    {
        words[j] = tmp;
    }
    tmp = "";
}
4
  • 3
    How is it working, but can't let you assign to the array? That means that something is not working. What does it do? Commented Jan 14, 2016 at 15:45
  • You should consider using string.Substring as you loop through the string keep track of the index of the last index for a space and then when you see a new one just substring from that index to the current. Also make sure to do a final substring after the loop. You can also keep track of the current word index to know where to put it in your words array. Commented Jan 14, 2016 at 15:48
  • based on your current code, you only need to keep track the current index of your word. See my answer as example. Don't forget the last assignment as it can be tricky. Commented Jan 14, 2016 at 15:50
  • 2
    But why can't you use string.Split? Commented Jan 14, 2016 at 15:50

3 Answers 3

5

You just need a kind of index pointer to put up your item one by one to the array:

string str = "Hello, how are you?";
string tmp = "";
int word_counter = 0;
for (int i = 0; i < str.Length; i++) {
    if (str[i] == ' ') {
        word_counter++;
    }
}
string[] words = new string[word_counter + 1];
int currentWordNo = 0; //at this index pointer
for (int i = 0; i < str.Length; i++) {
    if (str[i] != ' ') {
        tmp = tmp + str[i];
        continue;
    }
    words[currentWordNo++] = tmp; //change your loop to this
    tmp = "";
}
words[currentWordNo++] = tmp; //do this for the last assignment

In my example the index pointer is named currentWordNo

Sign up to request clarification or add additional context in comments.

2 Comments

@Omar Great, but I still wonder why can't you use string.Split which possibly the best available option? ;)
it is a homework :) and it is not allowed to use any inbuilt function
4

Try using regular expressions, like this:

  string str = "Hello, how are you?";

  // words == ["Hello", "how", "are", "you"] 
  string[] words = Regex.Matches(str, "\\w+")
    .OfType<Match>()
    .Select(m => m.Value)
    .ToArray();

String.Split is not a good option since there are too many characters to split on: ' ' (space), '.', ',', ';', '!' etc.

Word is not just a stuff between spaces, there are punctuations to consider, non-breaking spaces etc. Have a look at the input like this:

  string str = "Bad(very bad) input to test. . ."

Note

  1. Absence of space after "Bad"
  2. Non-breaking space
  3. Addition spaces after full stops

And the right output should be

  ["Bad", "very", "bad", "input", "to", "test"] 

Comments

0

You can also use a List to create your words list:

    string str = "Hello, how are you?";
    string tmp = "";
    List<string> ListOfWords = new List<string>();

    int j = 0;

    for (int i = 0; i < str.Length; i++)
    {
        if (str[i] != ' ')
        {
            tmp = tmp + str[i];
            continue;
        }
        // here is the problem, i cant assign every tmp in the array

        ListOfWords.Add(tmp);
        tmp = "";
    }
    ListOfWords.Add(tmp);

In this way you can avoid to count the number of word and the code is more simple. Use ListOfWord[x] to read any word

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.