int strLength = str.length()-1;
int x= 0;
while (x<strLength)
{
int j=0;
while (j < 4){
newString[j] = str[x];
cout<< newString[j];
j++;
}
cout << " ";
x++;
}
Should work. This is not really splitting the string! It's just outputting the string with spaces in it.
Greetz, Eddy
Eddy Dean
Junior Poster in Training
56 posts since Jul 2006
Reputation Points: 38
Solved Threads: 3
Wat is considered spliting a string? and also if i want to manipulate each new string, i would need to split the string?
Splitting a string is if you have one string, and separate it to 2 strings at a set location.
I cannot give you an example on this now because I'm not at home. Just google for string split c++.
Greetz,Eddy
Eddy Dean
Junior Poster in Training
56 posts since Jul 2006
Reputation Points: 38
Solved Threads: 3
Maybe something like
#include <iostream>
#include <string>
using namespace std;
const int tokenLength = 4;
int main (void)
{
string str = "12345678";
int strLength = str.length();
if (0 != (strLength % 4) )
exit (1);
char* temp = new char [5] ;
memset (temp, 5, '\0');
int sum = 0, i = 0;
int loop = (strLength / tokenLength );
char buffer [tokenLength];
while ( loop )
{
strncpy (buffer, str.c_str() + i * tokenLength , 4);
sum += atoi (buffer);
--loop;
++i;
}
printf("Final sum %d\n", sum);
return 0;
}
Though it is not very generic and picture perfect code, it just gives the general idea. YOu might want to optimize and clean it up a bit depending on your specific usage.
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Hope you can help. The following is essentially a small problem of an overall project, i have been trying to work out how to divide an inputted string, into say blocks of 4 characters and then output these separate blocks on the screen. e.g. 1234567891234567 ----> 1234 5678 9123 4567and so on. here's wat i have so far it seem to be working prob.
This is essentially a simple "output 4 characters, output space" loop....if i want to manipulate each new string, i would need to split the string?
This is a different problem.
Move each 4 characters into an array of new strings.in addition to splitting the char string, i need to convert it into its correspong integer e.g. "12345678" --> 12345678. then SPLIT the string and sum the blocks e.g. 1234 + 5678.
Yetanother problem. Maybe you''d better post your entire problem, not bits and pieces. It's extremely difficult to help when you keep changing the end result.
What is it you need to do?
What about this
int strLength = str.length()-1;
char temp[5] = { 0, 0, 0, 0, 0}; // isn't he using C++???
putchar(' '); // isn't he using C++???
printf("Final sum %d\n", sum); // isn't he using C++???
It would help to give him code in the language he needs it. Also, are we writing it for him or are we trying to help him write it himself?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I don't normally post complete homework solutions, but since everyone else is, here's my 2 cents worth
#include <iostream>
using namespace std;
int main()
{
const char str[] = "1234567891234567";
char array[5][5] = {0};
int nums[5] = {0};
char *ptr;
int numindex = 0;
int i, total;
ptr = array[0];
for(i = 0; str[i] != 0; i++)
{
if( (i > 0) && (i%4 == 0))
{
++numindex;
ptr = array[numindex];
}
*ptr++ = str[i];
nums[numindex] = (nums[numindex] * 10) + str[i] - '0';
}
++numindex;
total = 0;
for(i = 0; i < numindex; i++)
{
cout << nums[i] << " ";
total += nums[i];
}
cout << " total: " << total << "\n";
for(i = 0; i < numindex; i++)
{
cout << array[i] << " ";
}
cout << "\n";
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I just gave the solution with a different approach, but well the more the merrier. :)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
I don't normally post complete homework solutions, but since everyone else is, here's my 2 cents worth
I just gave the solution with a different approach, but well the more the merrier.
Sad... So sad...
Let's all give him his code. He can then just pick the one he likes and turn it in. Nothing solved, nothing gained... :confused:
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Sad... So sad...
Let's all give him his code. He can then just pick the one he likes and turn it in. Nothing solved, nothing gained...
Well Mr. WaltP, only because he posted his attempt did i help him out with his code since it shows that he has put in some effort and just needs the right technique. That way he can understand and put in his own optimizations and task specific details which is the same thing i advised him to do.Though it is not very generic and picture perfect code, it just gives the general idea. YOu might want to optimize and clean it up a bit depending on your specific usage.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Actually, since the op is apparently writing a c++ program (cout is c++) I wouldn't use any of the approaches presented so far -- use std::string and its substr() method to split the original string into 4 pieces. then use std::stringstream to convert them to integers. But maybe that is too advanced for him and may not meed the requirements of his assignment.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I figured you could handle the rest of your assignment yourself. But don't use what I posted without knowing what it did and how it was done -- most instructors will be able to recognize when a student just copied code from the web. There are as many ways to accomplish your project as there are programmers to write it. You instructor will know if you turn in an assignment that uses techniques beyone your current level of understanding.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343