C + + uses%20 to replace spaces in strings (O (n) Time efficiency)

Source: Internet
Author: User

#include <iostream>#include <malloc.h>#include <string.h>using namespace STD;//The spaces in the string are replaced with%20. voidGrial (Char*STR) {if(Str==null)return;Char*p = str;Char*q = NULL;Charbuf[strlen(str) +1];intCount =0;//Count the number of spaces.     while(*p!=' + ')    {if(*p=="') Count + +;    p++; }strcpy(BUF,STR);intn =strlen(str) +2*count+1; str = (Char*)malloc(n);memset(STR,' + ',sizeof(str));strcpy(STR,BUF); p = str+n-1;//n is the length of the string including ' s ', I want p to point to the next ' I ' position, and I let str+n point to the position behind ' /',                             //So p=str+n-1 is pointing to the next position ' ", why do I have to write this, I tangled for half a day how dislocation, Grandma's,                            //30 minutes without, after long memory. Q = p-count*2; while(1)    {if(*q = ="') {*p=' 0 ';            p--; *p=' 2 ';            p--; *p='% '; }Else{*p = *q; }if(P==Q) Break;        q--;    p--; }cout<<str<<endl;}intMain () {Chara[]="You were a good boy!!!"; Grial (a);return 0;}/*---------------------------------------------------------------------------------------*/#include <iostream>//To increment a value in a two-dimensional array. //A classmate of mine met this question in a 2-face written examination. using namespace STD;intGrial (int(*a) [6],intXintYintVal) {inti = y1;intj =0; while(i!=0&& j!=x) {if(A[j][i]>val)        {i--; }Else if(A[j][i]<val)        {j + +; }Else{returnA[j][i]; }    }return false; }intMain () {inta[][6]={1,3,4,6,7,9,2,4,5,7,9,Ten,4,5,7,8,Ten, -,                        };cout<<grial (A,3,6,3) <<endl;}

C + + uses%20 to replace spaces in strings (O (n) Time efficiency)

Related Article

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.