2014 trial-string replacement, 2014 string replacement

Source: Internet
Author: User

2014 trial-string replacement, 2014  string replacement

Requirement: enter a string, then enter an integer, that is, the number of times the string is replaced, and then enter the string to be replaced in sequence ......

For example:

Input: asdfghjasdfghj

3

As-> bnm

Df-> qwe

Gh-> yui

Output: bnmqweyuijbnmqweyuij

This means to replace as with bnm, df with qwe, and gh with yui in the input string. The total number of times is not limited, can be any integer.

If the number of input times is 2, the following is an example:

Input: asdfgasdfg

2

As-> bn

Df-> yuio

Output: bnyuiogbnyuiog

After finishing this question, I felt like I had screwed myself up. I chose to use the C language. In fact, students who have learned Java can use a few lines of code to implement it, consuming a lot of time.

The program implementation is as follows:


/*** 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;}

The running result is as follows:






Js string replacement

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 (""));

Who asked the second question?

No matter how hard I work, the years have accumulated, and I will never go back. If we say that memories can shake the tears of the night's dream into a Happy Melody, I think that the arrival of you, whether it is remembered or tears, should be my happiest rhythm. I once heard people say, the Wanderer must go through a wandering river bridge. This river bridge is not in love with secular fame and fortune, not much prosperous. It is just a lonely journey in the left and right, as long as you walk, that is the end of happiness.
 

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.