Trial-string replacement (aa-bc), aa-bc
The input string (the string only contains lowercase letters 'A' to 'Z') is output after cyclic conversion according to the following rules: a-> B, B-> c ,..., Y-> z, z-> a; if the input string contains two letters in a row, the last letter must be converted twice in a row. For example, if aa is converted to bc, zz is converted to AB. If there are more than two consecutive identical letters, the third letter is counted as the first one.
Required implementation functions:
Void convert (char * input, char * output)
[Input] char * input, the input string
[Output] char * output, the output string
#include<iostream>#include<string.h>using namespace std;char *m="bcdefghijklmnopqrstuvwxyza";void convert(char *input,char *output){ char *s=input; int len=strlen(s); char *o=output; int i=0; while(i<len) { o[i]=m[s[i]-'a']; if(i+1<len){ o[i+1]=m[s[i]-'a']; if(o[i]==o[i+1]) o[i+1]++; } i+=2; }}int main(int argc, char *argv[]){ string s; while(getline(cin,s)!=NULL) { const char *cs=s.c_str(); char *input=(char*)malloc(s.size()+1); strcpy(input,cs); char *output=(char*)malloc(s.size()+1); convert(input , output); printf("%s\n",output); free(input); free(output); } return 0;}
Test data:
A
Aa
Ccbfgg
Test results: