C ++ removes n places from the left of a string Loop

Source: Internet
Author: User
/**
* @ File 020_move_string.c
* @ Author dinghuaneng
* @ Date 2011.06.22
* @ Brief rotate the string to the left, that is, to move the circle to the left. Algorithm .
* The last method is very efficient in time and space, and Code Short, difficult to make mistakes.
* Source of the most space-saving and time-saving method: programming Pearl River
* @ Defgroup move_string
*@{
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

/********************** The most time-saving method *********** *************/
/**
* @ Brief rotate the string to N positions on the left
* @ Param STR string to be rotated
* @ Param [in] number of rotations required by mov
* @ Return none
*/
Void move_string_left (char * STR, int mov)
{
If (null = STR | mov <= 0)
Return;
Char TMP [mov];
Int I;
Int Len = strlen (STR );
If (LEN = 0)
Return;
MoV % = Len;
If (mov = 0)
Return;
For (I = 0; I <sizeof TMP; I ++)
TMP [I] = STR [I];
TMP [I] = '\ 0 ';
For (I = 0; I <len-mov; I ++)
STR [I] = STR [I + mov];
For (; I <Len; I ++)
STR [I] = TMP [I-(LEN-mov)];
}

/**
* @ Brief rotate the string n places to the right
* @ Param STR string to be rotated
* @ Param [in] number of rotations required by mov
* @ Return none
*/
Void move_string_right (char * STR, int mov)
{
If (null = STR | mov <= 0)
Return;
Char TMP [mov];
Int I;
Int Len = strlen (STR );
If (LEN = 0)
Return;
MoV % = Len;
If (mov = 0)
Return;
For (I = len-mov; I <Len; I ++ ){
TMP [I-(LEN-mov)] = STR [I];
}
TMP [I-(LEN-mov)] = '\ 0 ';
For (I = len-1; I> = mov; I --){
STR [I] = STR [I-mov];
}
For (; I> = 0; I --)
STR [I] = TMP [I];
}
/********************** The most time-saving method *********** *************/

/*********************** the most space-saving method ******** * **************/
/**
* @ brief: rotate the string to the left by 1 position.
* @ Param STR string to be rotated
* @ Param [in] the number of mov to be rotated
* @ return none
*/
void move_string_one_left (char * str)
{< br> If (null = Str)
return;
int Len = strlen (STR);
int I;
If (LEN = 0)
return;
char TMP = STR [0];
for (I = 0; I STR [I] = STR [I + 1];
}< br> STR [I] = TMP;
}

/**
* @ brief rotate the string at one position to the right
* @ Param STR string to be rotated
* @ Param [in] mov required number of rotations
* @ return none
*/
void move_string_one_right (char * Str)
{< br> If (null = Str)
return;
int Len = strlen (STR);
If (LEN = 0)
return;
char TMP = STR [len-1];
int I;
for (I = len-1; I> 0; I --) {
STR [I] = STR [I-1];
}< br> STR [I] = TMP;
}< br>/************************ the most space-saving method **** * *******************/

/********************** One of the most space-saving and time-saving methods ******** ****************/
/**
* @ Brief returns the maximum common divisor of values I and J.
* @ Return: returns the maximum common divisor correctly. If the parameter is incorrect, return-1.
*/
Int gcd (int I, Int J)
{
If (I <= 0 | j <= 0)
Return-1;
While (I! = J ){
If (I> J)
I-= J;
Else
J-= I;
}
Return I;
}

/**
* @ Brief rotate the string to N positions on the left
* @ Param STR string to be rotated
* @ Param [in] number of rotations required by mov
* @ Return none
*/
Void move_string_fast_left (char * STR, int mov)
{
If (null = STR | mov <= 0)
Return;
Int Len = strlen (STR );
Char TMP;
If (! Mov)
Return;
MoV % = Len;
If (! Mov)
Return;
Int I, J, K;
Int g_cd = gcd (mov, Len );
For (I = 0; I <g_cd; I ++ ){
TMP = STR [I];
J = I;
While (1 ){
K = J + mov;
If (k> = Len)
K-= Len;
If (k = I)
Break;
STR [J] = STR [k];
J = K;
}
STR [J] = TMP;
}
}

/**
* @ Brief rotate the string n places to the right
* @ Param STR string to be rotated
* @ Param [in] number of rotations required by mov
* @ Return none
*/
Void move_string_fast_right (char * STR, int mov)
{
If (null = STR | mov <= 0)
Return;
Int Len = strlen (STR );
If (! Mov)
Return;
MoV % = Len; // number of mobile repairs
If (! Mov)
Return;
MoV = len-mov;
Move_string_left (STR, mov );
}
/********************** One of the most space-saving and time-saving methods ******** ****************/

/********************** Method 2 of the most space-saving and time-saving methods ******** ****************/
/**
* @ Brief refers to two elements whose lengths start from pos1 and start from pos2 and are num.
* Avoid out-of-bounds memory!
* @ Param STR string of some characters to be exchanged
* @ Param [in] starting position of the first part of pos1
* @ Param [in] starting position of the second part of pos2
* @ Param [in] num number of characters to be exchanged decompiled and cracked by limit exa
*/
Void swap_string (char * STR, int pos1, int pos2, int num)
{
Char * str1 = STR + pos1;
Char * str2 = STR + pos2;
Int I;
Char TMP;
For (I = 0; I <num; I ++ ){
TMP = * str1;
* Str1 = * str2;
* Str2 = TMP;
Str1 ++;
Str2 ++;
}
}

/**
* @ Brief rotate to the left using the method of switching elements (shift to the left of the loop)
* @ Param STR string to be rotated
* @ Param [in] number of rotations required by mov
* @ Return none
*/
Void move_string_swap_left (char * STR, int mov)
{
If (null = STR | mov <= 0)
Return;
Int Len = strlen (STR );
If (! Mov)
Return;
MoV % = Len; // number of mobile repairs
If (! Mov)
Return;
Int I = mov;
Int J = len-mov;
While (I! = J ){
If (I> J ){
Swap_string (STR, mov-I, mov, J );
I-= J;
}
Else {
Swap_string (STR, mov-I, mov-I + J, I );
J-= I;
}
}
Swap_string (STR, mov-I, mov, I );
}
/********************** Method 2 of the most space-saving and time-saving methods ******** ****************/

/*********************** the third method to save space and time ***** * *****************/
/**
* @ brief starts from start end-to-End characters are reversed
* @ Param STR string of some characters to be reversed
* @ Param [in] Start position
* @ Param [in] end position
* @ return none
*/
void reverse (char * STR, int start, int end)
{< br> char * pos1 = STR + start;
char * pos2 = STR + end;
char TMP;
while (pos1 TMP = * pos1;
* pos1 = * pos2;
* pos2 = TMP;
pos1 ++;
pos2 --;
}< BR >}

/**
* @ brief uses the reverse method to rotate the string to the left (loop to the left)
* @ Param STR string to be rotated
* @ Param [in] mov Number of strings to be rotated
* @ return none
*/
void move_string_reverse_left (char * STR, int mov)
{< br> If (null = STR | mov <= 0)
return;
int Len = strlen (STR );
If (! Mov)
return;
mov % = Len; // number of moves repaired
If (! Mov)
return;
reverse (STR, 0, mov-1);
reverse (STR, mov, len-1);
reverse (STR, 0, len-1 );
}< br>/************************* the third method to save space and time * * **********************/
/*** @} */

# If 1
int main (INT argc, char ** argv)
{< br> char STR [] = "Hello world! ";
int n = atoi (argv [1]);
int I;
move_string_reverse_left (STR, N );
printf ("% s \ n", STR);
return 0;
}< BR ># endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.