Προς το περιεχόμενο

Mia ergasia se C Language.


Barf

Προτεινόμενες αναρτήσεις

Δημοσ.

Να μια ανάλογη ρουτίνα με αυτή του Directx.....

Σίγουρα έχει bugs γιατί νομίζω δεν καλύπτει όλες τις περιπτώσεις με αυτές που καλύπτει ο Directx αλλά ήταν μια γρήγορη φιλότιμη προσπάθεια. Αχ αυτά τα Stacks ακόμα τα παλεύω..

>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int FindWordinBuffer(char *buffer, char *word)
{
if(buffer && word)
{
	int i = 0;
	int index = 0;
	int index2 = 0;
	int found = 0;
	char *pbuffer = NULL;
	if((pbuffer = strstr(buffer,word)) != NULL)
	{
		//pbuffer is the pointer to the first occurace of word
		index = buffer - pbuffer;
		//Get the abs of the result
		printf("Index = %d\n",index);
		if(index < 0)
			index = -index;
		if(index == 0)
		{
				if(buffer[index+strlen(word)] == ' ' && !strncmp(&buffer[index],word,strlen(word)))
				{
						printf("Found the word <%s> in the given buffer in position %d\n",word,index);
						return 0;
				}
				else
				{
						printf("Word not found.\n");
						return -1;
				}
		}
		else
		{
			if(buffer[index-1] == ' ' && buffer[index+strlen(word)] == ' ' && !strncmp(&buffer[index],word,strlen(word)))
			{
					printf("Found the word <%s> in the given buffer in position %d\n",word,index);
					return 0;
			}
			else if(buffer[index-1] == ' ' && buffer[index+strlen(word)] == '\0' && !strncmp(&buffer[index],word,strlen(word)))
			{
					printf("Found the word <%s> in the given buffer in position %d\n",word,index);
					return 0;
			}
			else 
			{
				printf("Word not found.\n");
				return -1;	
			}

		}
	}
	else
	{
		printf("Word not found.\n");
		return -1;
	}
}
else
{
	printf("Null Parametres.\n");
	return -2;
}
}


Δημοσ.

Μια δεύτερη απλούστερη εκδοχή χρήσης της strstr δίχως τα προβλήματα και την πολυπλοκότητα της προηγούμενης υλοποίησης μου είναι να περικλείσουμε την προς αναζήτηση πρόταση μεταξύ κενών (ένα κενό στην αρχή, τουλάχιστον ένα κενό στο τέλος).

Και ομοίως να βάλουε ένα κενό στην αρχή της λέξης και ένα (όχι περισσότερα) στο τέλος της, έτσι ώστε να εξαναγκάσουμε, με τον απλούστερο τρόπο, την strstr να εντοπίζει μόνο λέξεις (string μεταξύ κενών δηλαδή) αγνοώντας κάθε περίπτωση εντοπισμού substring.

 

Ακολουθεί ο νέος απλούστερος κώδικας:

 

>
/*-Find Word 2 (directx)-------------------------------------------------------*/ 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#ifdef __BORLANDC__    /* CodeGear/Borland magic  */ 
#pragma hdrstop 
#endif 

/*---------------------------------------------------------------------------*/ 
int    _FindWord(char*,char*); 

#ifdef __BORLANDC__ /* CodeGear/Borland magic  */ 
#pragma argsused 
#endif 
int main(int argc, char* argv[]) 
{ 
   char    *pszLine = NULL, 
			*pszWord = NULL;
   int         nResult; 

   /* 
           Allocate two BUFSIZ buffers to store Line & Word. 
           (BUFSIZ on CodeGear Turbo C++ is equal to 512bytes)  
   */ 

   if((pszLine=(char*)malloc(BUFSIZ))==NULL) 
    printf(" Not enough memory for Line buffer!\n"); 
   else 
    if((pszWord=(char*)malloc(BUFSIZ))==NULL) 
       printf(" Not enough memory for Word buffer!\n"); 
    else 
       { 
           memset(pszLine,0,BUFSIZ-1); 
           memset(pszWord,0,BUFSIZ-1); 

           /* 
                   Ask user input.. 
           */ 
		printf(" Enter Line:"); gets(pszLine);
           printf(" Enter Word:"); gets(pszWord); 

           /* 
           Call _FindWord, if ==-1 then "Word Not Found" else "Word-Position". 
           */ 
		if((nResult=_FindWord(pszLine,pszWord))!=-1)
            printf(" Word found at %d\n",nResult); 
           else 
            printf(" Word not found!\n"); 
       } 

   /* 
           Resources clean-up! 
   */ 
   free(pszLine); free(pszWord); 

   /* 
       User exit confirmation! 
   */ 
   printf(" Press Enter to quit.."); 
   fgetc(stdin); 

   /* 
	Always exit with no-error (0).
*/
return 0;
}
/*---------------------------------------------------------------------------*/
int	_FindWord(char *pszLine,char *pszWord)
{
/*
	FindWord (V2) return the first occurance of pszWord in pszLine.

	Logic: Put pszLine in Trailing & Leading Spaces +
			 Put pszWord in Trailing & Leading Spaces.

			 Thus we force strstr to search for a whole Word and ignore
			 substring parts.
*/
int	 nPosition = 0;
char *pLine     = NULL,
	 *pWord     = NULL,
	 *pPosition = NULL;

if(!strlen(pszLine) || !strlen(pszWord)) return -1;

/* Allocate enough buffer to hold pszLine and pszWord plus their transformations */
if((pLine=(char*)malloc(strlen(pszLine)+3))==NULL)
 return -2;
if((pWord=(char*)malloc(strlen(pszWord)+3))==NULL)
 {
	free(pWord);
	return -2;
 }

	/* Buffers initilization */
memset(pLine,0,strlen(pszLine)+2);
memset(pWord,0,strlen(pszWord)+2);

/*
	Logic
			pLine should be = "one space+pszLine+at least one space"
			pWord should be = "one space+pszWord+one space"
*/

/* Insert a Leading  Space into Line */
strcat(pLine," ");
/* Copy Line without Leading Spaces  */
strcat(pLine,&pszLine[strspn(pszLine," ")]);
/* Insert a Trailing Space into Line */
strcat(pLine," ");

/* Insert a Leading  Space into Word */
strcat(pWord," ");
/* Copy Word without Leading Spaces  */
strcat(pWord,&pszWord[strspn(pszWord," ")]);
/* Reverse word ... */
strrev(pWord);
/* Copy Word without Trailing Spaces */
strcpy(pWord,&pWord[strspn(pWord," ")]);
/* Reverse word ... */
strrev(pWord);
/* Insert a Trailing Space into Word */
strcat(pWord," ");

/* If under debug, print intermediate buffers... */
#ifdef _DEBUG
 printf(" pLine = \"%s\"\n pWord = \"%s\"\n",pLine,pWord);
#endif

/* Guarantee case-insensitive checking */
strlwr(pWord);
strlwr(pLine);

/* Search string with strstr */
nPosition = -1;
if((pPosition=strstr(pLine,pWord))!=NULL)
 nPosition = (strstr(pLine,pWord)-pLine)+1;

/* Free local resources */
free(pLine);
free(pWord);

/* Return result */
return nPosition;
}

 

Υ.Γ.

Η αναζήτηση αυτή την φορά είναι case-insensitive, δηλαδή δεν διακρίνει μεταξύ μικρών και κεφαλαίων.

 

Όπως πάντα μπορεί να υπάρχουν bugs..

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...