exp_sent.c

MudOSa4DGD/mudlib/dgd/sfun/exp_sent.c · 1.1 KB

From the archive MudOSa4DGD-30.06.98.tar.gz

Download the archive   « Back to the archive contents

/*
 * exp_sent.c
 *
 * SFUN: explode a sentence into an array of words
 *
 * (C) Frank Schmidt, Jesus@NorseMUD
 *
 */


static string *explode_sentence(string str) {
  int i, e, sz, last_was_number;
  string *result;
  
  e = 0;
  last_was_number = 0;
  result = ({ "" });
  sz = strlen(str);
  for (i=0; i < sz; ++i) {
    int ch;
    if (((ch=str[i]) >= 'a' && ch <= 'z') ||
	(ch >= 'A' && ch <= 'Z')) {
      /* add letter in words */
      result[e] += str[i..i];
      last_was_number = 0;
    }
    else if (ch >= '0' && ch <= '9') {
      /* add characters in numbers (both ints and floats) */
      result[e] += str[i..i];
      last_was_number = 1;
    }
    else if (last_was_number && ch == '.') {
      /* add characters in floats */
      result[e] += str[i..i];
      last_was_number = 0;
    }
    else {
      /* add one more word */
      ++e;
      if (ch != ' ') {
	/* special characters, stored as a single word */
	result += ({ str[i..i], "" });
	++e;
      }
      else
	/* space */
	result += ({ "" });
      last_was_number = 0;
    }
  }
  /* return result */
  return result;
}

Site Stats

Snippets: 1791
Codebases: 571