Splitting a string?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Splitting a string?

 
0
  #1
Aug 24th, 2006
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

  1. int strLength = str.length()-1;
  2. int x= 0;
  3. while (x<strLength)
  4. {
  5. int j=0;
  6. while (j < 4){
  7. newString[j] = str[x];
  8. cout<< newString[j];
  9. j++;
  10. }
  11. x++;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Splitting a string?

 
0
  #2
Aug 24th, 2006
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)
Last edited by andor; Aug 24th, 2006 at 10:53 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: Splitting a string?

 
0
  #3
Aug 24th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Splitting a string?

 
0
  #4
Aug 24th, 2006
Wat is considered spliting a string? and also if i want to manipulate each new string, i would need to split the string?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Splitting a string?

 
0
  #5
Aug 24th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: Splitting a string?

 
1
  #6
Aug 24th, 2006
Originally Posted by hay_man View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Splitting a string?

 
1
  #7
Aug 24th, 2006
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
  1.  
  2. for (int x=0; x<strLength; x++)
  3. {
  4. if (0 == (x % 4) && 0 != x)
  5. putchar(' ');
  6. putchar(str[x]);
  7. }
Last edited by andor; Aug 24th, 2006 at 11:03 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Splitting a string?

 
0
  #8
Aug 24th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Splitting a string?

 
0
  #9
Aug 24th, 2006
Originally Posted by hay_man View Post
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
Last edited by andor; Aug 24th, 2006 at 11:24 am. Reason: Added let me think
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Splitting a string?

 
0
  #10
Aug 24th, 2006
What about this
  1. int strLength = str.length()-1;
  2. char temp[5] = { 0, 0, 0, 0, 0};
  3. int sum = 0;
  4.  
  5. for (int x=0; x<strLength; x++)
  6. {
  7. if (0 == (x % 4) && 0 != x)
  8. {
  9. putchar(' ');
  10. sum += atoi(temp);
  11. }
  12. temp[x % 4] = str[x];
  13. putchar(str[x]);
  14. }
  15. putchar('\n');
  16. if ((strLength % 4) == 0)
  17. sum += atoi(temp);
  18. printf("Final sum %d\n", sum);
Bye
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC