we have a strin which is stored in a text file...we have to read that string from text file ....them we have to make the fragments of a string and we have to count tht word number of thye string...finally we have to put this string into the structure....

like string is i love to spend holidays in india
we have to break strin in i...love...to...spend etc and have to calculate the word number of them like i has 1....love has 2...to has 3...
we have to make a array of an object of a structure..........
plzzzzzzzz help....i am a electronics student and mah project is dependent on this...plz help me...

Recommended Answers

All 11 Replies

the answer in this queastion is the programme similar to that in the book by "Bjhane Straustrup". u may easily refer it . it is possibly in the page no 341.

If you don't want to spend $$$ to buy that book just to look up the answer to your question, you could try using stringstream class, or use std::string's find method in a loop to locate each of the spaces

while not done
    check if the string has a space
    if not, then this loop is done
    call [b]substr[/b] to extract the word into another std::string object and insert this into a vector of strings.
    call [b]substr[/b] again to remove the word from the original string
 end of loop

the answer in this queastion is the programme similar to that in the book by "Bjhane Straustrup". u may easily refer it . it is possibly in the page no 341.

i dont have that book....can u please post it here...

Welcome aboard. If you have specific problems when trying to do a program...please post the source code here along with what you think may be wrong. Also post the errors you get, so we can take a look and help you. We are not in the business of doing your home-work for you! You must TRY/show some effort!!

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
struct packet
{
	char idenstr[60];
    int sentno;
	int fragno;
    int total;
	char payload[60];
	
	
}pkt[20];

const int max=60;
int main()
{


struct packet
{
	int fragno;
	char payload[60];
	int sentno;
	int total;
}pkt[20];


	char str1[max],str2[max],s2[max];
	FILE *f;
		f=fopen("ee.txt","r");
	fgets(str1,max,f);
	fgets(str2,max,f);
int x,m= 0,p,n=0,wordno=0,total1=0;

for(x=0;str1[x]!='\0';x++)
{
        if(str1[x]== ' ')
        {
			
                   for(p=m;p <= x;p++)
				   {
                           printf("%c",str1[p]);
						   strcpy(s2[p],str1[p]);
						   

						   
				   }
			
				   wordno = wordno+1;
				   total1=total1+1;
				   
				   pkt[n].fragno=wordno;   
				   n=n+1;
				  
				   printf("%d",wordno);


                   m = x + 1;
        }
 ;
printf("\n"); 
}

for(p = m; p <= x;p++)
{  
printf("%c",str1[p]); 

}
wordno=wordno+1;
total1=total1+1;
pkt[n].fragno=wordno;   
n=n+1;
printf("%d",wordno);



int x1,m1= 0,p1,total2=0;

for(x1=0;str2[x1]!='\0';x1++)
{
        if(str2[x1]== ' ')
        {
			
                   for(p1=m1;p1 <= x1;p1++)
				   {
                           printf("%c",str2[p1]);
						   

						   
				   }
				   wordno = wordno+1;
				   total2=total2+1;
				   

				   pkt[n].fragno=wordno;
				   n=n+1;
				   printf("%d",wordno);


                   m1 = x1 + 1;
        }
 ;
printf("\n"); 
}

for(p1 = m1; p1 <= x1;p1++)
{  
printf("%c",str2[p1]); 

}
wordno=wordno+1;
total2=total2+1;

pkt[n].fragno=wordno;  
 n=n+1;

printf("%d",wordno);

for(int g=0;g<n;g++)
{

printf("\n%d" ,pkt[g].fragno);

}
pkt[n].total=total1;

printf("\n%d" ,pkt[n].total);
pkt[n].total=total2;
printf("\n%d" ,pkt[n].total);


return(0);


}

this is not working because we have ti implement it usin g strings...and i am not able to do this...my whole socket programming project is complete...but i am not able to form packets...

Member Avatar for iamthwee

Why you shouldn't use structs with packets

http://www.daniweb.com/forums/post471114-2.html

And just incase anyone wants to give your code the eye over I tagged it up. After all the mods are too lazy to do it. :)

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>

struct packet
{
  char idenstr[60];
  int sentno;
  int fragno;
  int total;
  char payload[60]; 
}
pkt[20];

const int max = 60;
int main()
{ 
  struct packet
  {
    int fragno;
    char payload[60];
    int sentno;
    int total;
  }
  pkt[20]; 
  char str1[max], str2[max], s2[max];
  FILE *f;
  f = fopen ( "ee.txt", "r" );
  fgets ( str1, max, f );
  fgets ( str2, max, f );
  int x, m = 0, p, n = 0, wordno = 0, total1 = 0;

  for ( x = 0; str1[x] != '\0'; x++ )
  {
    if ( str1[x] == ' ' )
    {
      for ( p = m; p <= x; p++ )
      {
        printf ( "%c", str1[p] );
        strcpy ( s2[p], str1[p] ); 
      }

      wordno = wordno + 1;
      total1 = total1 + 1;

      pkt[n].fragno = wordno;
      n = n + 1;

      printf ( "%d", wordno ); 
      m = x + 1;
    };
    printf ( "\n" );
  }

  for ( p = m; p <= x; p++ )
  {
    printf ( "%c", str1[p] );
  }
  wordno = wordno + 1;
  total1 = total1 + 1;
  pkt[n].fragno = wordno;
  n = n + 1;
  printf ( "%d", wordno ); 

  int x1, m1 = 0, p1, total2 = 0;

  for ( x1 = 0; str2[x1] != '\0'; x1++ )
  {
    if ( str2[x1] == ' ' )
    {
      for ( p1 = m1; p1 <= x1; p1++ )
      {
        printf ( "%c", str2[p1] ); 
      }
      wordno = wordno + 1;
      total2 = total2 + 1; 
      pkt[n].fragno = wordno;
      n = n + 1;
      printf ( "%d", wordno ); 
      m1 = x1 + 1;
    };
    printf ( "\n" );
  }

  for ( p1 = m1; p1 <= x1; p1++ )
  {
    printf ( "%c", str2[p1] );
  }
  wordno = wordno + 1;
  total2 = total2 + 1;

  pkt[n].fragno = wordno;
  n = n + 1;

  printf ( "%d", wordno );

  for ( int g = 0; g < n; g++ )
  {
    printf ( "\n%d" , pkt[g].fragno );
  }
  pkt[n].total = total1;

  printf ( "\n%d" , pkt[n].total );
  pkt[n].total = total2;
  printf ( "\n%d" , pkt[n].total ); 
  return ( 0 ); 
}

this is not working....can u tell me only the code to break the string.....plzzz

And just incase anyone wants to give your code the eye over I tagged it up. After all the mods are too lazy to do it. :)

Ohhh that was really mean :@ We (or me at least) do lead normal lives like everyone else. I for one do not sit at my computer 24/7 waiting for someone to post some code without code tags.

here's one way to do it.

#include <string>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
    string word;
    vector<string> strlist;
    stringstream stream;
    stream << "i love to spend holidays in india";
    while( stream >> word)
    {
        strlist.push_back(word);
    }
    return 0;    
}

can any one tell me how to open a file in c++ because i have to take the input from thje txt file......fopen and gets are not working in c++

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

int main () 
{
    string str = "this is ee 450 class";

    string::iterator It = str.begin();

    while ( It != str.end() )
    {
    	if ( *It == ' ' )
    		*It = '\n';
    	cout << *It++;
    }
    cout << endl;
    
    return 0;
}

can any one tell me how to open a file in c++ because i have to take the input from thje txt file......fopen and gets are not working in c++

Really now -- when was the last time you read your textbook? search the c++ code snippets there are hundreds of examples there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.