/* Project ReplaceString Function to replace substrings in strings Version 1.0 / 2002.04.01 Autor: pstrainer@gmx.net */ #include #include #include #define line_maxlen 100 #define myversion 1 // valid options: {1,2} int replace_string(char*,char*,char*); int replace_sub (char*,char*,int,int); void main (void) { char mystring[line_maxlen]; char src[line_maxlen]; char dst[line_maxlen]; int sl1,sl2,found; printf("Projekt ReplaceString\n"); sl1=1; while (sl1>0) { printf("\nGeben sie einen Text ein: "); gets(mystring); sl1=strlen(mystring); if (sl1) { printf("Suche nach: "); gets(src); sl2=strlen(src); if (sl2) { printf("Ersetzen durch: "); gets(dst); printf("Original-String: <%s>\n",mystring); printf("<%s> wird ersetzt durch <%s>\n",src,dst); found=replace_string(mystring,src,dst); printf("Ergebnis-String: <%s>\n",mystring); printf("%d Teilstrings ersetzt\n",found); } } } } int replace_string(char *x,char *src,char *dst) { /* Funktion replace_string Ersetzen von Zeichenketten (substrings) in Zeichenketten (strings) Version 1.0 / 2002.04.01 Autor: pstrainer@gmx.net Syntax: int replace_string(char*,char*,char*); ... char string[...]; char src[...]; char dst[...]; int n; ... n=replace_string(string,src,dst); Return = Number of substrings replaced Length: 30 lines of code (this function) 8 lines of code (sub version 1) 13 lines of code (sub version 2) 38-43 lines of code (total) */ char c,d; int i,j,anf,lensrc,counter; lensrc=strlen(src); counter=i=j=0; anf=-1; c='$'; while (c!='\0') { c=x[i]; // string character d=src[j]; // src character if (d=='\0') { // end of src ? if (anf>=0) { i=replace_sub(x,dst,anf,lensrc); counter++; } anf=-1; j=0; } if (c==d) { // match ? if(anf<0) {anf=i;} // start of match j++; // next match char } else if (anf>=0) { // no match ? anf=-1; j=0; } i++; // next string character } return counter; } // *************** Version 1 ******************** #if myversion==1 int replace_sub (char *x,char *z, int p, int q) { // cut q characters at position p // insert string ins into string x at position p // return next position to investigate // Length: 13 lines of code char buffer [line_maxlen]; int i,j; // copy end of x to buffer i=p+q; j=0; // strcpy(buffer,x+p+q); while (x[i]!='\0') {buffer[j++]=x[i++];} buffer[j]='\0'; // copy dst to x i=p; j=0; // strcat( x, z ); while (z[j]!='\0') {x[i++]=z[j++];} // copy end to x j=0; // strcat(x,buffer); while (buffer[j]!='\0') {x[i++]=buffer[j++];} x[i]='\0'; return p+q-1; // return position to continue } // *************** Version 2 ******************** #elif myversion==2 int replace_sub (char *x,char *z, int p, int q) { // cut q characters at position p // insert string ins into string x at position p // return next position to investigate // Length: 8 lines of code char buffer [line_maxlen]; strcpy( buffer, x+p+q); // copy end of x to buffer x[p]='\0'; // clip x strcat( x, z ); // copy dst to x strcat(x,buffer); // copy end to x return p+q-1; // return position to continue } // *************** Version ? ******************** #else int replace_sub (char *x,char *z, int p, int q) { printf("\n*** Makro #myversion wurde illegal definiert ***\n\n"); return 0; } #endif /* ========== eof replace_string.cpp ========== */