Aller au contenu

Photo

I need a script that returns the number of word in a string.


  • Veuillez vous connecter pour répondre
2 réponses à ce sujet

#1
ShadowM

ShadowM
  • Members
  • 768 messages

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



#2
Squatting Monk

Squatting Monk
  • Members
  • 446 messages
#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.



#3
ShadowM

ShadowM
  • Members
  • 768 messages

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


  • Squatting Monk aime ceci