7 Replies - 931 Views - Last Post: 22 October 2007 - 04:02 AM Rate Topic: -----

#1 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

String and File Manipulation

Post icon  Posted 21 October 2007 - 10:12 PM

i need to check how many types a particular word occurs in a text file and at which location all the time,i.e.everytime that word occurs,the program should tell how many bytes are read before this word occurs.......plzzzzz help me......it's really urgent
Is This A Good Question/Topic? 0
  • +

Replies To: String and File Manipulation

#2 harshakirans  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 124
  • Joined: 26-April 06

Re: String and File Manipulation

Posted 21 October 2007 - 10:35 PM

We cannot post you with the code, Unless you show some effort.
So post the code whatever you have tried.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: String and File Manipulation

Posted 21 October 2007 - 10:39 PM

Well the two things you will need is an input stream for the file and the string function find() which you can use to locate words. Loop through the file and read each line, search for a word and add it to a counter if found. Then start your search after the position you found the word at and continue to see if there are more than one occurence.

Here is a great site to show you how to search for strings within strings...

Find a string within a string

Then lookup ifstream and you will be well on your way. :)
Was This Post Helpful? 0
  • +
  • -

#4 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: String and File Manipulation

Posted 21 October 2007 - 10:49 PM

View Postharshakirans, on 21 Oct, 2007 - 10:35 PM, said:

We cannot post you with the code, Unless you show some effort.
So post the code whatever you have tried.



i m using this code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  string input;
  int i = 0;
  int appearances = 0;
  ifstream myfile ("c:\\try.txt");
  if (myfile.is_open())
  {
	while (! myfile.eof() )
	{
	  getline (myfile,line,'~');
	  cout <<line<<endl;
	}
	for(i = line.find("world", 0); i != string::npos; i = line.find("world", i))
	{
		myfile.seekg(0,ios::end);
	   appearances++;
	   i++;  
	}
	cout<<appearances<<" times."<<endl;
	myfile.close();
  }
return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: String and File Manipulation

Posted 21 October 2007 - 11:32 PM

When you post code, please use code tags like this: :code:

What is the problem that you are having with this? The for loop is a little clunky in this instance, and I probably would have used a while loop instead, but it appears that it should give you the number of instances.

And the variable i prior to the increment step in your loop represents the number of characters read until that particular instance of the string you are looking for. Since 1 char is 1 byte, that gives you the number of bytes until the instance of the search string. If it's the number of bytes read until the instance that you can't get, you may want to just store the value of i in an array for later use.

But other than that, it should work. Please clarify the problems you are having if this didn't cover them.

-jjh

This post has been edited by jjhaag: 21 October 2007 - 11:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: String and File Manipulation

Posted 22 October 2007 - 12:25 AM

View Postjjhaag, on 21 Oct, 2007 - 11:32 PM, said:

When you post code, please use code tags like this: :code:

What is the problem that you are having with this? The for loop is a little clunky in this instance, and I probably would have used a while loop instead, but it appears that it should give you the number of instances.

And the variable i prior to the increment step in your loop represents the number of characters read until that particular instance of the string you are looking for. Since 1 char is 1 byte, that gives you the number of bytes until the instance of the search string. If it's the number of bytes read until the instance that you can't get, you may want to just store the value of i in an array for later use.

But other than that, it should work. Please clarify the problems you are having if this didn't cover them.

-jjh



i know yaar....i just did it..n it's workin...newyz.thanks a lot
Was This Post Helpful? 0
  • +
  • -

#7 garima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 09-October 07

Re: String and File Manipulation

Post icon  Posted 22 October 2007 - 03:34 AM

i need to do a program in which i can search for a particular word in a string without using any string function.i did that using a string funcn find() like this:::



#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string line;
int i = 0;
long l,m;
float perc;
int appearances = 1;
ifstream myfile ("c:\\try.txt");


if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line,'~');
cout <<line<<endl;
l = myfile.tellg();
myfile.seekg (0, ios::end);
m = myfile.tellg();
cout<<"Total size of file is: "<<m-1<<" bytes\n";
}
for(i = line.find("the", 0); i != string::npos; i = line.find("the", i))
{
perc=(100*i)/(m-1);
cout<<appearances<<" 'the' found when "<<perc<<" % of the file is read"<<endl;
appearances++;
i++;
}

myfile.close();
}
return 0;
}


now plzzzzzzz somebody help me..it's really very urgent
Was This Post Helpful? 0
  • +
  • -

#8 harshakirans  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 124
  • Joined: 26-April 06

Re: String and File Manipulation

Posted 22 October 2007 - 04:02 AM

Call this function with parameters matchfun(string,matchstr); in your main program ...The logic goes this way.


char string[100]="this is a easy task";
char matchstr[10]="is";
matchfun(char *p,char *r)
{
int i,len;
r=matchstr;
p=string;
len=strlen(r);
while(*p!='\0')
{
if(*p==*r)
for(i=0;i<len;i++)
if(*p==*r)
{
p++;
r++;
if(i==len-1)
{
printf("the string match at position %d",p-len);
exit(0);
}
}
p++;
}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1