944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1402
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 16th, 2007
0

urgent....need to make fragments of string

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notorious_pan is offline Offline
5 posts
since Nov 2007
Nov 16th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jbstin is offline Offline
11 posts
since Nov 2007
Nov 16th, 2007
0

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

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
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 16th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by jbstin ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notorious_pan is offline Offline
5 posts
since Nov 2007
Nov 16th, 2007
0

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

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!!
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Nov 16th, 2007
0

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

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notorious_pan is offline Offline
5 posts
since Nov 2007
Nov 16th, 2007
0

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

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.

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 16th, 2007
0

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

this is not working....can u tell me only the code to break the string.....plzzz
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notorious_pan is offline Offline
5 posts
since Nov 2007
Nov 16th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 16th, 2007
0

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

here's one way to do it.
c++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Urgent help in this code plz
Next Thread in C++ Forum Timeline: i need help......





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC