/*
Project InsertString
Function to insert a substring into a string
Version 1.0 / 2002.04.01
Autor: pstrainer@gmx.net
*/
#include
#include
#include
#define line_maxlen 100
#define myversion 3 // valid options: {1,2}
void insert_string(char*,char*,int);
void main (void) {
char mystring[line_maxlen];
char insertstring[line_maxlen];
int position,sl;
printf("Projekt InsertString\n");
sl=1;
while (sl>0) {
printf("\nGeben sie einen Text ein: ");
gets(mystring);
sl=strlen(mystring);
if (sl) {
printf("Einsetzen: ");
gets(insertstring);
printf("Nach Position: ");
scanf("%d",&position);
fflush(stdin);
printf("Original-String: <%s>\n",mystring);
insert_string(mystring,insertstring,position);
printf("Ergebnis-String: <%s>\n",mystring);
}
}
}
// *************** Version 1 ********************
#if myversion==1
void insert_string (char *x,char *z, int p) {
/*
Funktion insert_string
Insert string z into string x at position p
Version 1.0 / 2002.04.01
This version realized with library functions:
Autor: Peter Schindler, pstrainer@gmx.net
Syntax:
char mystring[...],insertstring[...];
int position;
... ...
insert_string(mystring,insertstring,position);
Length:
9 lines of code
*/
char buffer [line_maxlen];
if ((p>=0) && (p<=int(strlen(x)))) { // validity test
strcpy( buffer, x+p); // save end in buffer
x[p]='\0'; // clip x
strcat( x, z ); // copy dst to x
strcat(x,buffer); // copy end to x
}
}
// *************** Version 2 ********************
#elif myversion==2
void insert_string (char *x,char *z, int p) {
/*
Funktion insert_string
Insert string z into string x at position p
Version 2.0 / 2002.04.01
This version realized without library functions (using only "strlen"):
Autor: Peter Schindler, pstrainer@gmx.net
Syntax:
char mystring[...],insertstring[...];
int position;
... ...
insert_string(mystring,insertstring,position);
Length:
14 lines of code
*/
char buffer [line_maxlen];
int i,j;
if ((p>=0) && (p<=int(strlen(x)))) { // validity test
// save end in buffer
i=p;j=0; // strcopy (buffer,x+p)
while(x[i]!='\0') {buffer[j++]=x[i++];}
buffer[j++]='\0'; // terminate buffer
// 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'; // terminate string
}
}
// *************** Version 3 ********************
#elif myversion==3
int my_strlen(char *);
void insert_string (char *x,char *z, int p) {
/*
Funktion insert_string
Insert string z into string x at position p
Version 3.0 / 2002.04.01
This version realized without library functions:
Autor: Peter Schindler, pstrainer@gmx.net
Syntax:
char mystring[...],insertstring[...];
int position;
... ...
insert_string(mystring,insertstring,position);
Length:
16 lines of code
*/
int i,j;
if ((p>=0) && (p<=int(my_strlen(x)))) { // validity test
i=my_strlen(x); // shift end
j=i+my_strlen(z);
while (j>p) {x[j--]=x[i--];}
i=p; // insert
j=0;
while (z[j]!='\0') {x[i++]=z[j++];}
}
}
int my_strlen(char *x) {
// calculate the length of a string.
// does the same as library function "strlen"
int i=0;
while (x[i]!='\0') {i++;}
return i;
}
// *************** Version ? ********************
#else
void insert_string (char *x,char *z, int p) {
printf("\n *** Illegal value for macro variable myversion ***\n\n");
}
#endif
/* ========== eof insert_string.cpp ========== */
               (
geocities.com/pstrainer/entwicklung/c)                   (
geocities.com/pstrainer/entwicklung)                   (
geocities.com/pstrainer)