C :: Aufgabe #19

3 Lösungen Lösungen öffentlich

Text nach Wörtern untersuchen

Anfänger - C von Dome - 29.12.2012 um 01:37 Uhr
Ein Text und das Wort was gesucht werden soll, soll eingegeben werden. Daraufhin soll ausgegeben wie oft das Wort in dem Text vorhanden ist.

Konsolenausgabe:

Eingabe Text: Das ist mein Beispieltext.
Welches Wort soll gesucht werden: ist

Das Wort "ist" wurde 1 mal gefunden.

Lösungen:

vote_ok
von devnull (8870 Punkte) - 08.12.2013 um 19:59 Uhr
Quellcode ausblenden C-Code
/********************************************
 * word_search.c   search word in string
 * devnull         08-12-2013                
 ********************************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define STR_LEN   1000
#define WRD_LEN   100

char* getword(char *str, char *wrd, char *pos)
{
    static char *p; 
    char *w;

	w = NULL;
	p = (pos)?pos:p;
    while (*p != '\0' && !isalpha(*p))
        p++;
    if (*p != '\0') {
		w = p;
		while (isalnum(*p))
			p++;
		*p++ = '\0';
	}
	return w;
}

int count_word(char *str, char *wrd)
{
	char *sdp, *wp;
	int cw = 0;
	
	if ((sdp = strdup(str)) != NULL) {
		wp = sdp;
		while ((wp = getword(sdp, wrd, wp)) != NULL) {
			if (strcmp(wp, wrd) == 0)
				cw++;
			wp = NULL;
		}
		free(sdp);
	}
	return cw;
}

int main( )
{
    char text[STR_LEN+1];
    char word[WRD_LEN+1];

	printf("Text: " );
    if (gets(text) != NULL)
		if (strlen(text) > 0) {
			printf("Wort: ");
			scanf("%s", word);
			printf("Das Wort '%s' wurde %d mal gefunden.\n", word, count_word(text, word));
		}
	return 0;
}
vote_ok
von devnull (8870 Punkte) - 15.12.2013 um 11:58 Uhr
Quellcode ausblenden C-Code
/********************************************
 * word_search2.c  search word in string
 *                 using strtok(3)
 * devnull         08-12-2013                
 ********************************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define STR_LEN   1000
#define WRD_LEN   100

int count_word(char *str, char *wrd)
{
	char *sdp, *wp;
	int cw = 0;
	
	if ((sdp = strdup(str)) != NULL) {
        for (wp = sdp ;; wp = NULL) {
			wp = strtok(wp, " \t\n");
            if (wp == NULL)
				break;
			if (strcmp(wp, wrd) == 0)
				cw++;
		}
		free(sdp);
	}
	return cw;
}

int main( )
{
    char text[STR_LEN+1];
    char word[WRD_LEN+1];

	printf("Text: " );
    if (gets(text) != NULL)
		if (strlen(text) > 0) {
			printf("Wort: ");
			scanf("%s", word);
			printf("Das Wort '%s' wurde %d mal gefunden.\n", word, count_word(text, word));
		}
	return 0;
}
vote_ok
von metin1994 (40 Punkte) - 20.07.2014 um 18:06 Uhr
Quellcode Wortsuche in Textdatei
Quellcode ausblenden C#-Code
#include <stdio.h>
#include <stdlib.h>
#define dateiname_read "U:\\Desktop\\informatik1\\26.06.14_Wortsuche\\faust.txt"

int main()
{
int findpos=-1, pos=1;
char wort [80];
char suchwort [80];
FILE *fpr;
  printf("\nWortsuche\n");
  printf("==========\n");
  // Suchanfrage
  printf("Bitte geben sie das zu suchende Wort ein:");
  scanf("%s", suchwort);
  printf("\ngesucht wird %s\n", suchwort);
  // Datei öffnen
  if((fpr=fopen(dateiname_read,"r"))==(FILE*)NULL)
  {
      printf("kein Lesezugriff auf die Datei!\n");
      exit (1);
  }
  // Datei einlesen
  while (!feof(fpr))
  {
      fscanf(fpr,"%s",wort);
      if (!strcmp(wort, suchwort))
      {
          findpos=pos;
          break;
      }
      pos++;
  }
  fclose (fpr);
  if (findpos==-1)
  {
      printf ("\ndas Wort wurde nicht gefunden.\n");
  }
  else
  {
      printf("\n\ndas Wort kommt an Position %d vor.\n", findpos);
  }
  return;
}