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 <strong>substr</strong> to extract the word into another std::string object and insert this into a vector of strings.
call <strong>substr</strong> again to remove the word from the original string
end of loop
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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!!
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
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 );
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343