| | |
urgent....need to make fragments of string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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...
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...
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.
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
Last edited by stymiee; Nov 16th, 2007 at 4:31 pm. Reason: email removed
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
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#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...
Last edited by Ancient Dragon; Nov 16th, 2007 at 5:53 pm. Reason: add code tags
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.
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)
#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 ); }
Last edited by iamthwee; Nov 16th, 2007 at 3:39 pm.
*Voted best profile in the world*
•
•
•
•
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.
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.
here's one way to do it.
c++ Syntax (Toggle Plain Text)
#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; }
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.
![]() |
Similar Threads
- How to convert string to const char* in C (C++)
- my #include <string> wont make "string" bold (C++)
- check number is string (C)
- adding to a string (C++)
- How do i verify a string using hashtables (Java)
- need help in creating class string (C++)
Other Threads in the C++ Forum
- Previous Thread: Urgent help in this code plz
- Next Thread: i need help......
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






