2014華為機試-字串替換,2014華為字串替換

來源:互聯網
上載者:User

2014華為機試-字串替換,2014華為字串替換

題目要求:輸入一個字串,然後在輸入一個整數,就是替換字串的次數,然後依次輸入需要替換的字串……

例如:

輸入:asdfghjasdfghj

           3

           as->bnm

           df->qwe

           gh->yui

輸出:bnmqweyuijbnmqweyuij

意思就是,將輸入的字串中,as替換成bnm,df替換成qwe,gh替換成yui,總共替換三次,注意次數是不限定的,可以是任意整數等。

如果輸入的次數是2,舉例說明:

輸入:asdfgasdfg

           2

           as->bn

           df->yuio

輸出:bnyuiogbnyuiog

做完這道題,感覺自己把自己坑了,選擇了用C語言,其實學過Java的同學隨便都可以用幾行代碼實現,耗掉自己很多時間。

程式實現如下:

/*** Replace String*/#include <stdio.h>#include <string.h>#define SUCCESS0#define FAILED-1#define MAXLINE48#define NOTFOUND-1int GetNumber(void){char temp;int count;count = 0;temp = getchar();while(temp >= '0' && temp <= '9'){count = count * 10 + (temp - 0x30);temp = getchar();}return count;}void GetString(char buff[]){char temp;int i;i = 0;temp = getchar();while (temp != '\n'){buff[i++] = temp;temp = getchar();}buff[i] = '\0';}int FindString(int begin, char str1[], char str2[]){int i, j;int MAXSIZE = strlen(str2);i = begin;while (str1[i] != '\0'){for (j = 0; j < MAXSIZE; j++){if (str1[i++] == str2[j])continue;else break;}if (j == MAXSIZE)return (i - j);}return NOTFOUND;}int myStrcpy(char dest[], char src[]){int i = 0;while (src[i] != '\0'){dest[i] = src[i];i++;}dest[i] = '\0';return SUCCESS;}void StringSplite(char *pBuff, char target[], char repStr[]){int i = 0, j = 0;while(*(pBuff + i) != '-'){target[i] = *(pBuff + i);i++;}target[i] = '\0';while (*(pBuff + i) == '-' || *(pBuff + i) == '>')i++;while (*(pBuff + i) != '\0'){repStr[j++] = *(pBuff + i);i++;}repStr[j] = '\0';}void ReplaceString(char dest[], char src[], char repStr[]){char temp[MAXLINE] = {'\0'};int Swidth, Rwidth;int begin = 0, iPos = 0;int i = 0;Swidth = strlen(src);Rwidth = strlen(repStr);while (dest[i] != '\0'){iPos = FindString(begin, dest, src);if (iPos != NOTFOUND){myStrcpy(temp, dest + iPos + Swidth);myStrcpy(dest + iPos, repStr);myStrcpy(dest + iPos + Rwidth, temp);memset(temp, 0, MAXLINE);begin = iPos + Rwidth;i = begin;}elsebreak;}}int main(void){char **pChar = NULL;char inData[48] = {'\0'};char target[10] = {'\0'};char repStr[10] = {'\0'};int i, j, sum;GetString(inData);fflush(stdin);sum = GetNumber();pChar = (char **) malloc(sum * sizeof(char *));if (pChar == NULL){printf("Malloc memory for pChar has faild!\n");return 0;}for (i =0; i < sum; i++){*(pChar + i) = (char *) malloc(MAXLINE * sizeof(char));if (*(pChar + i) == NULL){printf("Malloc memory for *(pChar + %d) has faild!\n", i);return 0;}}for (i = 0; i < sum; i++){fflush(stdin);GetString(*(pChar + i));}for (i = 0; i < sum; i++){memset(target, 0, sizeof(target));memset(repStr, 0, sizeof(repStr));StringSplite(*(pChar + i), target, repStr);ReplaceString(inData, target, repStr);}printf("%s\n", inData);for (i = 0; i < sum; i++)free(*(pChar + i));free(pChar);return 0;}

運行結果如下:





js 字串替換問題

var a="abcdabcdabcdabcd".match(/abcd/g);
a[1]=a[1]?a[1].replace(/^a/,"x"):"";
a[2]=a[2]?a[2].replace(/^a/,"y"):"";
alert(a.join(""));
 
誰提問的第二個問題?

歲月沉澱出的流年,無論我怎樣努力都是荒蕪中的一段夢,過去了,再也回不去了。如果說;記憶能將午夜夢回的淚水撥動出幸福的旋律,我想;關於你的到來不管是想起還是淚水,都應該是我最幸福律動,曾聽人說,流浪者,必須要經過一條漂泊中的河橋,這座河橋不戀世俗名利,沒有太多的繁華,只是在左右孤單中寂寞的旅程,只要邁步走過,那便就是幸福的終點。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.