| | |
Splitting a string?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 16
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
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++; }
•
•
Join Date: Jul 2006
Posts: 56
Reputation:
Solved Threads: 3
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
•
•
Join Date: Jul 2006
Posts: 56
Reputation:
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
•
•
•
•
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

C Syntax (Toggle Plain Text)
for (int x=0; x<strLength; x++) { if (0 == (x % 4) && 0 != x) putchar(' '); putchar(str[x]); }
Last edited by andor; Aug 24th, 2006 at 11:03 am.
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Jun 2006
Posts: 16
Reputation:
Solved Threads: 0
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.
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)
What about this
Bye
C Syntax (Toggle Plain Text)
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);
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: String Conversion
- Next Thread: Event after Form_Load
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi





