全部博文(776)
分类: C/C++
2010-11-30 08:46:25
#include
#include
static void Swap ( char *p, char *q );
void StringShift ( char *str, int len, int n )
{
int i, tmp;
char *s = str;
if ( str == NULL )
{
return;
}
if ( len <= 0 || n <= 0 || n >= len )
{
return;
}
while ( n != len - n )
{
if ( n < len - n )
{
for ( i = 0; i < n; i++ )
Swap ( &s[i], &s[len - n + i] );
len -= n;
}
else
{
for ( i = len - 1; i > n - 1; i-- )
Swap ( &s[i], &s[i - n] );
s += len - n;
tmp = n;
n = n + n - len;
len = tmp;
}
} // end while
for ( i = 0; i < n; i++ )
{
Swap ( &s[i], &s[n + i] );
}
}
static void Swap ( char *x, char *y )
{
*x ^= *y ;
*y ^= *x ;
*x ^= *y ;
}
int main()
{
int i;
char A[] = {1, 2, 3, 4, 5, 6, 7};
StringShift ( A, 7, 1 );
for ( i = 0; i < 7; i++ )
{
printf ( "%d, ", A[i] );
}
return 0;
}