urgent....need to make fragments of string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 5
Reputation: notorious_pan is an unknown quantity at this point 
Solved Threads: 0
notorious_pan notorious_pan is offline Offline
Newbie Poster

urgent....need to make fragments of string

 
0
  #1
Nov 16th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 11
Reputation: jbstin is an unknown quantity at this point 
Solved Threads: 0
jbstin jbstin is offline Offline
Newbie Poster

Re: urgent....need to make fragments of string

 
0
  #2
Nov 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: urgent....need to make fragments of string

 
0
  #3
Nov 16th, 2007
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 substr to extract the word into another std::string object and insert this into a vector of strings.
    call substr again to remove the word from the original string
 end of loop
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: notorious_pan is an unknown quantity at this point 
Solved Threads: 0
notorious_pan notorious_pan is offline Offline
Newbie Poster

Re: urgent....need to make fragments of string

 
0
  #4
Nov 16th, 2007
Originally Posted by jbstin View Post
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...
Last edited by stymiee; Nov 16th, 2007 at 4:31 pm. Reason: email removed
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: urgent....need to make fragments of string

 
0
  #5
Nov 16th, 2007
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!!
I shot the sheriff....but I didn't shoot the deputy
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: notorious_pan is an unknown quantity at this point 
Solved Threads: 0
notorious_pan notorious_pan is offline Offline
Newbie Poster

Re: urgent....need to make fragments of string

 
0
  #6
Nov 16th, 2007
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. struct packet
  6. {
  7. char idenstr[60];
  8. int sentno;
  9. int fragno;
  10. int total;
  11. char payload[60];
  12.  
  13.  
  14. }pkt[20];
  15.  
  16. const int max=60;
  17. int main()
  18. {
  19.  
  20.  
  21. struct packet
  22. {
  23. int fragno;
  24. char payload[60];
  25. int sentno;
  26. int total;
  27. }pkt[20];
  28.  
  29.  
  30. char str1[max],str2[max],s2[max];
  31. FILE *f;
  32. f=fopen("ee.txt","r");
  33. fgets(str1,max,f);
  34. fgets(str2,max,f);
  35. int x,m= 0,p,n=0,wordno=0,total1=0;
  36.  
  37. for(x=0;str1[x]!='\0';x++)
  38. {
  39. if(str1[x]== ' ')
  40. {
  41.  
  42. for(p=m;p <= x;p++)
  43. {
  44. printf("%c",str1[p]);
  45. strcpy(s2[p],str1[p]);
  46.  
  47.  
  48.  
  49. }
  50.  
  51. wordno = wordno+1;
  52. total1=total1+1;
  53.  
  54. pkt[n].fragno=wordno;
  55. n=n+1;
  56.  
  57. printf("%d",wordno);
  58.  
  59.  
  60. m = x + 1;
  61. }
  62. ;
  63. printf("\n");
  64. }
  65.  
  66. for(p = m; p <= x;p++)
  67. {
  68. printf("%c",str1[p]);
  69.  
  70. }
  71. wordno=wordno+1;
  72. total1=total1+1;
  73. pkt[n].fragno=wordno;
  74. n=n+1;
  75. printf("%d",wordno);
  76.  
  77.  
  78.  
  79. int x1,m1= 0,p1,total2=0;
  80.  
  81. for(x1=0;str2[x1]!='\0';x1++)
  82. {
  83. if(str2[x1]== ' ')
  84. {
  85.  
  86. for(p1=m1;p1 <= x1;p1++)
  87. {
  88. printf("%c",str2[p1]);
  89.  
  90.  
  91.  
  92. }
  93. wordno = wordno+1;
  94. total2=total2+1;
  95.  
  96.  
  97. pkt[n].fragno=wordno;
  98. n=n+1;
  99. printf("%d",wordno);
  100.  
  101.  
  102. m1 = x1 + 1;
  103. }
  104. ;
  105. printf("\n");
  106. }
  107.  
  108. for(p1 = m1; p1 <= x1;p1++)
  109. {
  110. printf("%c",str2[p1]);
  111.  
  112. }
  113. wordno=wordno+1;
  114. total2=total2+1;
  115.  
  116. pkt[n].fragno=wordno;
  117. n=n+1;
  118.  
  119. printf("%d",wordno);
  120.  
  121. for(int g=0;g<n;g++)
  122. {
  123.  
  124. printf("\n%d" ,pkt[g].fragno);
  125.  
  126. }
  127. pkt[n].total=total1;
  128.  
  129. printf("\n%d" ,pkt[n].total);
  130. pkt[n].total=total2;
  131. printf("\n%d" ,pkt[n].total);
  132.  
  133.  
  134. return(0);
  135.  
  136.  
  137. }

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...
Last edited by Ancient Dragon; Nov 16th, 2007 at 5:53 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: urgent....need to make fragments of string

 
0
  #7
Nov 16th, 2007
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.

  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. struct packet
  7. {
  8. char idenstr[60];
  9. int sentno;
  10. int fragno;
  11. int total;
  12. char payload[60];
  13. }
  14. pkt[20];
  15.  
  16. const int max = 60;
  17. int main()
  18. {
  19. struct packet
  20. {
  21. int fragno;
  22. char payload[60];
  23. int sentno;
  24. int total;
  25. }
  26. pkt[20];
  27. char str1[max], str2[max], s2[max];
  28. FILE *f;
  29. f = fopen ( "ee.txt", "r" );
  30. fgets ( str1, max, f );
  31. fgets ( str2, max, f );
  32. int x, m = 0, p, n = 0, wordno = 0, total1 = 0;
  33.  
  34. for ( x = 0; str1[x] != '\0'; x++ )
  35. {
  36. if ( str1[x] == ' ' )
  37. {
  38. for ( p = m; p <= x; p++ )
  39. {
  40. printf ( "%c", str1[p] );
  41. strcpy ( s2[p], str1[p] );
  42. }
  43.  
  44. wordno = wordno + 1;
  45. total1 = total1 + 1;
  46.  
  47. pkt[n].fragno = wordno;
  48. n = n + 1;
  49.  
  50. printf ( "%d", wordno );
  51. m = x + 1;
  52. };
  53. printf ( "\n" );
  54. }
  55.  
  56. for ( p = m; p <= x; p++ )
  57. {
  58. printf ( "%c", str1[p] );
  59. }
  60. wordno = wordno + 1;
  61. total1 = total1 + 1;
  62. pkt[n].fragno = wordno;
  63. n = n + 1;
  64. printf ( "%d", wordno );
  65.  
  66. int x1, m1 = 0, p1, total2 = 0;
  67.  
  68. for ( x1 = 0; str2[x1] != '\0'; x1++ )
  69. {
  70. if ( str2[x1] == ' ' )
  71. {
  72. for ( p1 = m1; p1 <= x1; p1++ )
  73. {
  74. printf ( "%c", str2[p1] );
  75. }
  76. wordno = wordno + 1;
  77. total2 = total2 + 1;
  78. pkt[n].fragno = wordno;
  79. n = n + 1;
  80. printf ( "%d", wordno );
  81. m1 = x1 + 1;
  82. };
  83. printf ( "\n" );
  84. }
  85.  
  86. for ( p1 = m1; p1 <= x1; p1++ )
  87. {
  88. printf ( "%c", str2[p1] );
  89. }
  90. wordno = wordno + 1;
  91. total2 = total2 + 1;
  92.  
  93. pkt[n].fragno = wordno;
  94. n = n + 1;
  95.  
  96. printf ( "%d", wordno );
  97.  
  98. for ( int g = 0; g < n; g++ )
  99. {
  100. printf ( "\n%d" , pkt[g].fragno );
  101. }
  102. pkt[n].total = total1;
  103.  
  104. printf ( "\n%d" , pkt[n].total );
  105. pkt[n].total = total2;
  106. printf ( "\n%d" , pkt[n].total );
  107. return ( 0 );
  108. }
Last edited by iamthwee; Nov 16th, 2007 at 3:39 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: notorious_pan is an unknown quantity at this point 
Solved Threads: 0
notorious_pan notorious_pan is offline Offline
Newbie Poster

Re: urgent....need to make fragments of string

 
0
  #8
Nov 16th, 2007
this is not working....can u tell me only the code to break the string.....plzzz
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: urgent....need to make fragments of string

 
0
  #9
Nov 16th, 2007
Originally Posted by iamthwee View Post
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.
Last edited by Ancient Dragon; Nov 16th, 2007 at 6:08 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: urgent....need to make fragments of string

 
0
  #10
Nov 16th, 2007
here's one way to do it.
  1. #include <string>
  2. #include <vector>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string word;
  9. vector<string> strlist;
  10. stringstream stream;
  11. stream << "i love to spend holidays in india";
  12. while( stream >> word)
  13. {
  14. strlist.push_back(word);
  15. }
  16. return 0;
  17. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC