Hello,
How Do I Search A Text File For The String Which The User Entered?
Thank You For Your Help
Hello,
How Do I Search A Text File For The String Which The User Entered?
Thank You For Your Help
one way is to open the file using function(fopen and fclose from stdio.h) and search for your word using (do ........while) loop using (getchar()) function till the end of file, but u ll ve to detect the spaces and put every word(it ll be alphas between two spaces) in a temp variable, and check to c if its ur word.
good luck
Programming is a high logical enjoyable art for both programer and user !!
Here is an idea. A function can be created to search for a string within a string. If all the data in the text file is stored in a string array, then you're all set.
Here is the function...
#include<string.h>
#define yes 1
#define no 0
#define max 256 // for example
void nullify(char *string)
{
for(int i=0; i<max; i++)
string[i]=0;
}
bool instr(char *a,char *b)
{
bool value=no;
int *m=new int;
char *temp=new char[max];
for(int i=0; i<strlen(b); i++)
{
*m=0; nullify(temp);
for(int l=i; l<strlen(b); l++)
{
temp[*m]=b[l];
(*m)++;
if(stricmp(a,temp)==0)
{
value=yes;
break;
}
}
}
delete m;
delete temp;
return(value);
}
The instr function will return 1 if a is present in b, and 0 if it is not.
Compiler:Borland C++ 5.0
Last edited by sundeeptuteja; 07-03-2002 at 05:04 AM.