Sword Point 42nd-Expand: Left rotation string: Left rotation operation is to transfer several characters in front of the string to the end of the string, such as input "ABCDEFG" and the number 2, left rotation 2 bit after: "Cdefgab"
1 //============================================================================2 //Name:jz-c-42-plus.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description: Left rotation string: The left rotation operation is to transfer several characters in front of the string to the end of the string, such as input "ABCDEFG" and the number 2, left rotation 2 bit after: "Cdefgab"7 //============================================================================8 9#include <iostream>Ten#include <stdio.h> One#include"StringUtil.h" A#include <string.h> - using namespacestd; - the Char* Leftrotatestring (Char* PStr,intN) { - if(PStr! =NULL) { - intNlength = static_cast<int>(strlen (PSTR)); - if(Nlength >0&& n >0&& N <nlength) { + Char* Pfirststart =pStr; - Char* Pfirstend = pStr + N-1; + Char* Psecondstart = pStr +N; A Char* Psecondend = pStr + nlength-1; at //flips the front n characters of a string - Reverse (Pfirststart, pfirstend); - //flips the back part of a string - Reverse (Psecondstart, psecondend); - //Flip the entire string - Reverse (Pfirststart, psecondend); in } - } to + returnpStr; - } the * //==================== test Code ==================== $ voidTest (Char* TestName,Char* Input,intNumChar*Expectedresult) {Panax Notoginseng if(TestName! =NULL) -printf"%s begins:", testname); the + Char* result =leftrotatestring (input, num); A the if(Input = = NULL && Expectedresult = =NULL) +|| (Input! = NULL && strcmp (result, expectedresult) = =0)) -printf"passed.\n\n"); $ Else $printf"failed.\n\n"); - } - the //Functional Testing - voidTest1 () {Wuyi CharInput[] ="ABCDEFG"; the CharExpected[] ="Cdefgab"; - WuTest ("Test1", input,2, expected); - } About $ //Boundary Value Test - voidTest2 () { - CharInput[] ="ABCDEFG"; - CharExpected[] ="BCDEFGA"; A +Test ("Test2", input,1, expected); the } - $ //Boundary Value Test the voidTest3 () { the CharInput[] ="ABCDEFG"; the CharExpected[] ="Gabcdef"; the -Test ("Test3", input,6, expected); in } the the //Robustness Testing About voidTest4 () { theTest ("Test4"Null6, NULL); the } the + //Robustness Testing - voidTest5 () { the CharInput[] ="ABCDEFG";Bayi CharExpected[] ="ABCDEFG"; the theTest ("Test5", input,0, expected); - } - the //Robustness Testing the voidTest6 () { the CharInput[] ="ABCDEFG"; the CharExpected[] ="ABCDEFG"; - theTest ("Test6", input,7, expected); the } the 94 intMainintargcChar**argv) { the Test1 (); the Test2 (); the Test3 ();98 Test4 (); About Test5 (); - Test6 ();101 102 return 0;103}
Jz-c-42-plus