Author Topic: I need a script that returns the number of word in a string.  (Read 248 times)

Legacy_ShadowM

  • Hero Member
  • *****
  • Posts: 1373
  • Karma: +0/-0


               

I need a word count from string.


So for example


This is an example


Would return 4


 


or script that would turn the number of words into repeats of the same text.


so example the above would be turned into the following string. Thanks in advance.


test test test test



               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
I need a script that returns the number of word in a string.
« Reply #1 on: July 24, 2015, 05:24:32 am »


               

#include "x3_inc_string"

int GetWordCount(string sString)
{
    int    nCount;
    string sWord = StringParse(sString);

    while (sWord != "")
    {
        nCount++;
        sString = StringRemoveParsed(sString, sWord);
        sWord = StringParse(sString);
    }

    return nCount;

This is a pretty naive function. It won't work correctly for thing like "This.Is.A.Test" (the words are not separated by spaces) or "this is a . test" (since the punctuation is being caught as a word. Still, it's good for simple use cases where proper punctuation is used.



               
               

               
            

Legacy_ShadowM

  • Hero Member
  • *****
  • Posts: 1373
  • Karma: +0/-0
I need a script that returns the number of word in a string.
« Reply #2 on: July 24, 2015, 06:09:36 pm »


               

Thanks Squatting Monk


I figured out a clunky way of using the count to convert to number of words to number or new word. I using it in my language system with animals. I have added a 2da line to appearance.2da that you fill in what animal sound (type into a string, example cows will say mow and parrot will say Squawk) Now they will say it almost equal to the words they say and will translate if you can speak with animals. '<img'>