C + + string manipulation Pen Test second wave

Source: Internet
Author: User

//1. String substitution spaces: Implement a function that replaces each space in a string with "%20". //For example, enter "We are happy.", then output "we%20are%20happy." #include <iostream>#include <assert.h>#include <string.h>using namespace STD;Char* Grial (Char*s) {assert (s! = NULL);intLen =strlen(s);intCount =0;//Count the number of spaces.     Char*p = s; while(*p! =' + ')    {if(*p = ="') count++;    p++; }intn = len + count *2+1;Char*str =New Char[n];Char*ret = str;memset(STR,' + ', n);strcpy(str,s);//Copy the original string into the new string array. p = str + N-1;Char*q = str + len; while(Q < P) {if(*q = ="') {*p--=' 0 '; *p--=' 2 '; *p ='% '; }Else{*p = *q;        } q--;    p--; }returnRet }intMain () {CharS[] ="We are happy";cout<<grial (s) <<endl;return 0;}//2. Determines whether a string is a string after the rotation of another string. ///For example: given S1 = AABCD and S2 = Bcdaa, return 1, given S1 = ABCD//And S2 = ACBD, returns 0.#include <iostream>#include <assert.h>using namespace STD;BOOLGrial (Const Char*STR1,Const Char*STR2) {assert (str1!=null&&str2!=null);Const Char*p = str1;Const Char*q = str2 +strlen(STR2)-1;if(strlen(str1)! =strlen(STR2))return 0; while(*p! =' + ')    {if(*p! = *q)return 0;        p++;    q--; }return 1;}intMain () {CharS1[] ="ABCD";CharS2[] ="DCBA";cout<< grial (S1, S2) << Endl;return 0;} *///3. Defines the left rotation of a string: moves several characters in front of the string to the end of the string. //such as String abcdef left rotation 2 bit to get string cdefab. Please implement the function of string left rotation. //requires that the complexity of the string operation with length n is O (n) and the auxiliary memory is O (1). #include <iostream>#include <assert.h>using namespace STD;voidSwap (Char*P1,Char*P2) {CharTemp while(P1 < P2)        {temp = *P1;        *P1 = *P2;        *P2 = temp;        p1++;    p2--; }}voidGrial (Char*s,intN) {assert (s!=null);intLen =strlen(s);Char*p = s + Len-1;Char*q = s;Char*midchar = NULL; Swap (Q,P);//First overall reverse. Midchar = p-n+1;//Then split into two parts in reverse order. Swap (q,midchar-1); Swap (midchar,p);}intMain () {CharS[] ="ABCdef"; Grial (s),3);cout<< s << Endl;return 0;} *///4. Finding substrings: Simulating the implementation of the STRSTR function. #include <iostream>#include <assert.h>#include <string.h>using namespace STD;intMy_atoi (Char*P1,Char*P2) {intCount =0; while(P1 <= p2) {count = Count *Ten+ *P1-' 0 ';    p1++; }returnCount;}Char* MY_STRSTR (Char*dist,Char*SRC) {intn =strlen(SRC);intNUM1 = My_atoi (src,src+n-1);intFlags = num1% -;//Mark.     Char*p = dist;Char*q;Char*m; while(P <= Dist +strlen(Dist)-N) {m=p;if(*p = = *src) {if(My_atoi (p, p + N-1) % -= = Flags) {q = src; while(*q! =' + '&& *p!=' + ')                {if(*q! = *p) Break;                    q++;                p++; }if(*q = =' + ')returnM        }} m++;    P=m; }returnNULL;}intMain () {CharS1[] ="123456789";CharS2[] ="789";cout<<my_strstr (S1, S2) <<endl;return 0;}//5. Simulate the Atoi function that implements the library function. #include <iostream>#include <assert.h>using namespace STD;BOOLIsnum (CharCH) {if(CH-' 0 ') >=0&& (CH-' 0 ') <=9)return true;return false;}intGetnum (Const Char*s) {Const Char*p = s;intCount =0; while(*p! =' + ')    {if(*p = ='. ')returnCountif(count<0)return 2147483647; Count = Count *Ten+ *p-' 0 ';    p++; }}intMy_atoi (Const Char*s) {assert (s! = NULL);Charch = *s;CharFlagsif(Isnum (ch)) flags = Isnum (CH) +' 0 ';ElseFlags = CH;Const Char*p = s +1;Switch(flags) { Case' + ':returnGetnum (P); Break; Case '-':if(Getnum (p) = =2147483647)return 0-Getnum (p)-1;return 0-Getnum (p); Break; Case ' 1 ':returnGetnum (s); Break;default:Exit(2); }return 0;}intMain () {cout<< My_atoi (" -31321312321321") << Endl;return 0;}#include <iostream>#include <assert.h>using namespace STD;//Implement a memmove on your own, and take a deep look at memory coverage issues. void* My_memmove (void*dist,void*SRC,intLen) {assert (dist!=null&&src!=null);Char*pdist = (Char*) Dist;void*ret = dist;Char*PSRC = (Char*) src;if(pdist <= psrc | | psrc + len <= pdist) { while(len--)        {*pdist++ = *psrc++; }returnRet }Else{pdist + = len-1; PSRC + = Len-1; while(len--)        {*pdist--= *psrc--; }returnRet }}intMain () {intA[] = {2,3,4, $,1,6,7}; My_memmove (A +1A8);int*b =New int[7]; My_memmove (B,a, -);inti =0; for(; I <4; i++) {cout<< B[i] <<"  "; }inti =0; for(; I <7; i++) {cout<< A[i] <<"  "; }cout<< Endl;return 0;}#include <iostream>#include <assert.h>using namespace STD;voidMy_memcopy (void*a,Const  void*b,intLen) {assert (A! = Null&&b = NULL);Char*p = (Char*) A;Char*q = (Char*) b; while(len--)    {*p++ = *q++; }}intMain () {intA[] = {1,2,3,4,5};int*b =New int[5]; My_memcopy (b, A,sizeof(a));inti =0; for(; I <5; i++) {cout<< B[i] << Endl; }memcpy(B,a, -);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + string manipulation Pen Test second wave

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.