CODE
#include <string>
#include <conio.h>
# define MAX 100
using namespace std;
int main(void)
{
fstream File;
char Buffer[1024]; //alternatively string Buffer;
File.open("textfile.txt", ios::in);
char srch[MAX]; //for search word input
int occ=0,cnt=0;
int plc[100];
int ln=strlen(srch);
cout<<("\nSearch pattern: ");
cin.getline(srch,'.');
while(!File.eof())
{
File.getline(Buffer, sizeof(Buffer)); //alternatively: File >> Buffer;
cout << endl;
for(int i=0;i<strlen(Buffer);i++)
{
if(srch[0]==Buffer[i] && srch[ln-1]==Buffer[i+ln-1])
{for( int m=1; m<ln; m++)
{
if(srch[m]==Buffer[i+m])//check the inner letters
{
plc[cnt]=i; //set place equals to i
cnt++;
if(m==ln-1) //if all secussful till the endof lenght then incremt occ+
occ++;
}
else
{
occ=0; // else set occ to zero and count to zero
cnt=0;
break; //break loop
}
}
cout <<"String present: "<<srch<<"\n";
cout <<"Appeared: "<<occ<<" times"<<endl;
for(int k=0;k<occ;k++)
{
cout<<"Occurrence: "<<plc[k]<<endl;
}
}
getch();
}
if ( File == NULL )
cout<<"No match was found.\n";
File.close();
return (0);
}
}
iv'e edited smal bit of the program.. and still i can't the the value i wanted..
the program works fine when i did't put any textfile.txt... still wont work as wat ive wanted.. plz help...
program should run like this
Search pattern: tat
Output:
String present: tat
Appeared: 4 times
Occurrence 1: <position here>
Occurrence 2: <position here>
Occurrence 3: <position here>
Occurrence 4: <position here>
This post has been edited by dms02x: 16 Mar, 2008 - 04:37 PM