/*
Projekt WordCount

Ermittlung der Anzahl von Worten in einer Zeichenkette (string)

Versionen 1.0 und 2.0 / 2002.04.01
Autor: pstrainer@gmx.net
*/

#include 
#include 
#include 

#define line_maxlen 100
#define myversion 2		// valid options: {1,2}

int word_count(char*);

// *************** Version 1 ********************
#if myversion==1

void main (void) {
	int wc;
	char mystring[]="abc xyz N=123";

	wc=word_count(mystring);
	printf("String <%s> enthaelt %d Worte\n",mystring,wc);
}

int word_count(char *s) {
/*
Funktion word_count
Ermittlung der Anzahl von Worten in einer Zeichenkette (string)

Version 1.0 / 2002.04.01
Autor: Peter Schindler, pstrainer@gmx.net

Syntax:
	int wordcount(char*);
	...
	int wc;
	char mystring[...];
	...
	wc=word_count(mystring);
Length: 
14 lines of code
*/
	int i,wc;
	char c;

	i=0;				// index of char in string
	wc=0;				// word count
	c='$';
	while(c!='\0') {
		c=s[i++];		// character tested
		if(c==' ')wc++;
	}
	i--;
	if(i>0)wc++;
	return wc;
}

// *************** Version 2 ********************
#elif myversion==2

void main (void) {
	int sl,wc;
	char mystring[line_maxlen];

	printf("Projekt WordCount\n");
	sl=1;

	while (sl>0) {
		printf("Geben sie einen Text ein: ");
		gets(mystring);
		sl=strlen(mystring);
		wc=word_count(mystring);
		printf("   %d Worte gefunden\n",wc);
	}

#ifdef _DEBUG
	printf("\nPress any key to continue ");
	_getch();
#endif
}

int word_count(char *s) {
/*
Funktion word_count
Ermittlung der Anzahl von Worten in einer Zeichenkette (string)

Version 2.0 / 2002.04.01
Autor: Peter Schindler, pstrainer@gmx.net

> Ermittlung der Anzahl von Worten in einer Zeichenkette (string)
> Kontrollzeichen für Wort-Zwischenräume:
  Alle Zeichen <'0' sowie alle Zeichen '\x3A' - '\x3F'
> Falls ein string kein Ende-Zeichen enthält, so wird die Funktion nach 
  line_maxlen Zeichen abgebrochen.

Syntax:
	int wordcount(char*);
	...
	int wc;
	char mystring[...];
	...
	wc=word_count(mystring);
Length:
18 lines of code
*/
	int i,wc,wl;
	char c;

	i=0;					// index of char in string
	wc=0;					// word count
	wl=0;					// word length
	c='$';
	while((c!='\0')&&(i='\x3A') && (c<='\x3F') ) ) {	
			if(wl>0) wc++;	// count word
			wl=0;			// reset word length
		}
		else wl++;			// word length
	}
	if(wl>0)wc++;
	return wc;
}

// *************** Version ? ********************
#else

void main (void) {
	printf("Makro #myversion wurde illegal definiert\n");
}

#endif

/* ========== eof ========== */

    Source: geocities.com/pstrainer/entwicklung/c/aufgaben

               ( geocities.com/pstrainer/entwicklung/c)                   ( geocities.com/pstrainer/entwicklung)                   ( geocities.com/pstrainer)