GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2005, 03:46
fj8283888 fj8283888 is offline
Junior Member
 
Join Date: Apr 2004
Posts: 37
fj8283888 is on a distinguished road

shift text


Hi:

I have a value char a[12]="123456";

If I have to add "0" before 123456 to become "000000123456" what should I do?
  #2  
Old 04-Nov-2005, 03:58
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: shift text


Hi 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:
          a[ i ] = a[ i - 1 ];
i.e make the character equal to the previous character.
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  
Old 04-Nov-2005, 10:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: shift text


Quote:
Originally Posted by fj8283888
Hi:

I have a value char a[12]="123456";

If I have to add "0" before 123456 to become "000000123456" what should I do?
That would be illegal:

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:
#include <stdio.h>
#include <string>

int main()
{
  char a[12] = "123456";
  char b[]   = "000000123456";

  int sizea;
  int sizeb;
  int lengtha;
  int lengthb;

  sizea = sizeof(a);
  lengtha = strlen(a);
  sizeb = sizeof(b);
  lengthb = strlen(b);

  printf("The size of array a is %d\n", sizea);
  printf("The length of the \"string\" in a is %d\n\n", lengtha);
  printf("The size of array b is %d\n", sizeb);
  printf("The length of the \"string\" in b is %d\n", lengthb);

  return 0;
}

Code:
The size of array a is 12 The length of the "string" in a is 6 The size of array b is 13 The length of the "string" in b is 12


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  
Old 04-Nov-2005, 18:18
fj8283888 fj8283888 is offline
Junior Member
 
Join Date: Apr 2004
Posts: 37
fj8283888 is on a distinguished road

Re: shift text


CPP / C++ / C Code:
#include <iostream>
#include <cstring>
using namespace std;
void Convert(int, char*);   

void main ()
{
char test[14]="1222349.10";

td=strlen(test);
int l=0;
char string[5];

do
{

l++;

}while (test[l]!='.');
test[13]=test[l+2];
test[12]=test[l+1];
test[11]=test[l];
int tes=0;
tes=12-l;//empty space
int lp=10;
int diff=0;
l--;
do
{
test[lp]=test[l--];
diff=lp-l;
lp--;

}while (0<=l);
cout<<diff;
int a=0;
diff--;
do
{
test[a]='0';
a++;
}while (diff>a);
cout << test;




}

I guess it should be a better way to perform
  #5  
Old 04-Nov-2005, 23:57
frug frug is offline
New Member
 
Join Date: Oct 2005
Posts: 9
frug is on a distinguished road

Re: shift text


Here are a couple of examples that might help you.

CPP / C++ / C Code:
	// char buffers
	char szA[13] = _T("123456");
	int nPos  = lstrlen(szA) - 1;
	int nInsertPos = sizeof(szA) - 2;
	memset(szA + nPos + 1, '0', nInsertPos - nPos);
	while(nPos >= 0)
	{
		szA[nInsertPos--] = szA[nPos];
		szA[nPos--] = '0';
	}
	TRACE1(_T("%s\n"), szA);

	// If you are initialy dealing with a number, then it is trivial
	char szB[13] = {};
	int n = 123456;
	wsprintf(szB, _T("%12.12d"), n);
	TRACE1(_T("%s\n"), szB);

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  
Old 05-Nov-2005, 00:07
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: shift text


Hi 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:
char test[14]="1222349.10";
1. Find the length of the character array first.
CPP / C++ / C Code:
    int length = strlen(test);
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:
          a[ i ] = a[ i - 1 ];
i.e make the character equal to the previous character.
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:
    test[0] = '0';

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:
    for(int j = 0; j < 13 - length; j++)
    {
        for(int i = 13; i > 0 ; i--)
        {
            test[ i ] = test[ i - 1 ];
        }
    test[0] = '0';
    }

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:
    for( length = 0; test[ length ] != NULL ; length++ )
    ;

And also note that main must return an int rather than being void.

Summarising, here is the complete code:
CPP / C++ / C Code:
#include<iostream>
using namespace std;

int main()                                      //main MUST return an int.
{
    char test[ 14 ] = "1222349.10";             //a string test
    int length;                                 //length is used to hold the length of the string. 

    for( length = 0; test[ length ] != NULL ; length++ )
    ;                                           //calculate length using a loop.       
    
    
    for(int j = 0; j < 13 - length; j++)        //repeat the shifting 13 - length times.       
    {
         
        for(int i = 13; i > 0 ; i--)            //shift the characters.
        {
            test[ i ] = test[ i - 1 ];          //make previous character equal to the present character
        }
        
    test[0] = '0';                              //make first character to be 0
    }
    
    cout << test << endl;                       //print the string now and see the output
                
    return 0;                                   //return 0 indicates successful execution of the program.
}


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  
Old 06-Nov-2005, 09:28
Dominic Dominic is offline
Member
 
Join Date: Aug 2005
Posts: 123
Dominic will become famous soon enough

Re: shift text


Seems a lot of user code, where simple formating by sprintf might work. Try this example :
CPP / C++ / C Code:
double Number = 123456.235;
	char Buff[50];
	sprintf(Buff,"%f",Number);
	printf("\nNumber = %s\n",Buff);

	sprintf(Buff,"%d",static_cast <int> (Number));
	printf("\nNumber = %s\n",Buff);


	sprintf(Buff,"% 020d",static_cast <int> (Number));
	printf("\nNumber = %s\n",Buff);


	sprintf(Buff,"% 020f",Number);
	printf("\nNumber = %s\n",Buff);


	sprintf(Buff,"% 020.3f",Number);
	printf("\nNumber = %s\n",Buff);



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 GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 02:36.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.