/* Program to demonstrate string handling Version 1.0 / 2001.09.01. Peter Schindler pstrainer@gmx.net */ #include #include #include #define MAXLEN 1000 /* Declaration of globals *****************************/ void nchr (int repeat_n, char char_c, char into_x[]); void addstrg(char from_a[],char and_b[],char into_c[]); /* Main program *************************************/ void main (void){ char c1,c[MAXLEN],d[MAXLEN]; int i,j; // init string for (i=0;i<=MAXLEN;i++) {c[i]='\0';} // user interface printf("String test program:\n"); printf("string #1 is entered from console\n"); printf("string #2 is created repeating a given character n-times\n"); printf("then, both strings are combined\n\n"); printf("Enter string#1: "); scanf("%s",&c); printf("Enter an integer number: "); scanf("%d",&j); printf("Enter a character: "); c1=_getche(); printf("\n\n"); printf("string #1=<%s>\n",c); nchr(j,c1,d); printf("string #2=<%s>\n",d); addstrg(c,d,c); printf("combined =<%s>\n",c); printf("End of program\n"); _getch(); } void nchr (int n, char c, char x[]) { int i; if (n<0) {n=0;} if (n>MAXLEN) {n=MAXLEN;} for (i=0;i<=n;i++) {x[i]=c;} x[i]='\0'; } void addstrg(char a[],char b[],char c[]) { int i,j; i=0; while (a[i]!='\0') { c[i]=a[i]; i++; if (i>=MAXLEN) {break;} } j=0; while(b[j]!='\0') { c[i++]=b[j++]; if (i>=MAXLEN) {break;} } c[i]='\0'; } /* eof ************************************************/