Hi,
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.

i think i must cout<<" " ?

thanks,
hay_man

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++;
    }
    x++;
}

You don't need double loop. What you need is one foor loop wich will print str[x] and putchar(' '); if its if (0 == (x % 4) && 0 != x)

[LEFT]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[/LEFT]

Wat is considered spliting a string? and also if i want to manipulate each new string, i would need to split the string?

the output from ur suggestion, which i had tried originally is;
1111 2222 3333 4444 5555 6666 7777 8888 i am trying to creat 1234 5678 9123 4567.
thanks again

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

the output from ur suggestion, which i had tried originally is;
1111 2222 3333 4444 5555 6666 7777 8888 i am trying to creat 1234 5678 9123 4567.
thanks again

I can't belive it. How is this possible :)

for (int x=0; x<strLength; x++)
   {
      if (0 == (x % 4) && 0 != x)
         putchar(' ');
      putchar(str[x]);  
   }

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. I have written the code to convert the string into an integer value. i must actually split the string not just cout a string with blanks. I assume i must create a series of new integers to store each block then its just a matter of creatin some intger, sum, and sum each individual block.

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. I have written the code to convert the string into an integer value. i must actually split the string not just cout a string with blanks. I assume i must create a series of new integers to store each block then its just a matter of creatin some intger, sum, and sum each individual block.

OK but this is not what you said in first post. Let me think

What about this

int strLength = str.length()-1; 
   char temp[5] = { 0, 0, 0, 0, 0};
   int sum = 0;

   for (int x=0; x<strLength; x++)
   {
      if (0 == (x % 4) && 0 != x)
      {
         putchar(' ');
         sum += atoi(temp);
      }
      temp[x % 4] = str[x];
      putchar(str[x]);  
   }
   putchar('\n');
   if ((strLength % 4) == 0)
      sum += atoi(temp);
   printf("Final sum %d\n", sum);

Bye

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.

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.

Yet another 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?

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;
}

I just gave the solution with a different approach, but well the more the merrier. :)

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:

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.

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.

thanks to all however i am using c++ and ancient dragon did not post the whole solution. will be home l8er, shall post my entire prob then.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.