![]() |
|
#1
|
|||
|
|||
shift textHi:
I have a value char a[12]="123456"; If I have to add "0" before 123456 to become "000000123456" what should I do? |
|||
|
#2
|
||||
|
||||
Re: shift textHi fj8283888,
Here is a hint how to solve it: 1. Find the length of the character array first. 2. We should use two loops. 3. One to shift the characters to the right and the other one to check how many times we should shift the characters. 4. To shift the characters to the right, use like this: CPP / C++ / C Code:
Note that this should be done within the array length limit. i value should be between 1 and 11 only in your case. 5. After shifting them to the right, make the first character to be '0'. 6. Suppose the array length is 12(since a[12]) and the actual size is 6, then you must shift 12-6 times. i.e 6 times. Hope this helps you a bit. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: shift textQuote:
The array a has memory enough for twelve chars. The string "000000123456" takes 13 chars (one for each of the ascii values of the digits and one for the terminating zero byte). This is a really, realllllly important point that even some experienced programmers forget sometimes. CPP / C++ / C Code:
Code:
If you want to shift, Paramesh has given you a way, but it is up to you, the programmer, to make sure there is enough room in the array. Regards, Dave |
|
#4
|
|||
|
|||
Re: shift textCPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: shift textHere are a couple of examples that might help you.
CPP / C++ / C Code:
Note that the memset is not important for the example string we are shifting, but if the length of string we are shifting is less then half, then the memset is critical. If not used the resulting string would replace the original string with '0's. I hope the examples helped. Felix |
|
#6
|
||||
|
||||
Re: shift textHi fj8283888 ,
There is no need to check for a '.' inside the string. You can simply shift the numbers and add zero without checking for any other things. Here is how we do it: The array is like this: CPP / C++ / C Code:
CPP / C++ / C Code:
3. One to shift the characters to the right and the other one to check how many times we should shift the characters. 4. To shift the characters to the right, use like this: CPP / C++ / C Code:
Note that this should be done within the array length limit. i value should be between 1 and 13 only in your case. 5. After shifting them to the right, make the first character to be '0'. CPP / C++ / C Code:
6. Since the array length is 14(since a[12]) and the actual size is 10, then you must shift 13-10 times. i.e 3 times. Dave explained us why it is 13 and not 14. Here is the loop: CPP / C++ / C Code:
We can also find the length of the array by using a loop like this instead of including the cstring header. CPP / C++ / C Code:
And also note that main must return an int rather than being void. Summarising, here is the complete code: CPP / C++ / C Code:
You can also do this program using do while loops and while loops instead of for loops. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#7
|
|||
|
|||
Re: shift textSeems a lot of user code, where simple formating by sprintf might work. Try this example :
CPP / C++ / C Code:
I used several examples, and the printf statement is just there if you want to see the result. The sprintf function alone will format the number into the character array. Note the size of Buff. Its always better to be bigger than too small. With modern computers, there is usaully plenty of memory. But over run one array bounds, just once and anything could happen, including a very long time to debug the problem. |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to read unknown total of int data from text file to a 2-dim array in C or C++? | ladyscarlet99 | C Programming Language | 2 | 09-Sep-2005 14:07 |
| need help on text file analyser | ffantasy | C Programming Language | 1 | 05-Sep-2005 13:44 |
| saving html text | dopee | MySQL / PHP Forum | 1 | 17-Jan-2005 04:15 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The