C
Chris Schumacher
I'm working on a program which splits a long string into a series of
smaller ones. It is supposed to work as follows;
1. You enter a string into variable "str"
2. The program searches for the space character and cuts everything
between the start-point and that space into a segment of the "thread"
array.
3. The program prints out the string again by printing out each
occupied segment of the array, split up by a space.
As of this moment the program returns only the first word and shuts
down. I think it might be a problem with the While loop, but I don't
see what's wrong, or really any other way to make it work.
[Note: the problem is in the While loop, I can assure you, I dummied
the For loop and checked out the array with a series of cout
statements, and it stops copying data into the array after the first
entry]
Complete code follows, any help is greatly appreciated.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
string str, thread[100];
cout << "Pull the string!";
cin >> str;
while(strLocale != string::npos)
{
strLocale = str.find(" ",strBegin);
strLngth = strLocale-strBegin;
thread = str.substr(strBegin,strLngth);
strBegin = strLocale + 1;
strCount++;
i++;
};
for (j = 1; j <= strCount; j++)
cout << thread[j] << " ";
return 0;
}
-==Kensu==-
smaller ones. It is supposed to work as follows;
1. You enter a string into variable "str"
2. The program searches for the space character and cuts everything
between the start-point and that space into a segment of the "thread"
array.
3. The program prints out the string again by printing out each
occupied segment of the array, split up by a space.
As of this moment the program returns only the first word and shuts
down. I think it might be a problem with the While loop, but I don't
see what's wrong, or really any other way to make it work.
[Note: the problem is in the While loop, I can assure you, I dummied
the For loop and checked out the array with a series of cout
statements, and it stops copying data into the array after the first
entry]
Complete code follows, any help is greatly appreciated.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
string str, thread[100];
cout << "Pull the string!";
cin >> str;
while(strLocale != string::npos)
{
strLocale = str.find(" ",strBegin);
strLngth = strLocale-strBegin;
thread = str.substr(strBegin,strLngth);
strBegin = strLocale + 1;
strCount++;
i++;
};
for (j = 1; j <= strCount; j++)
cout << thread[j] << " ";
return 0;
}
-==Kensu==-