This program is to insert a string inside another from a given position.Code:#include <stdio.h> void readLine (char buffer[]) //function to read what I type in the terminal { int i = 0, character; do { character = getchar (); buffer[i] = character; ++i; } while (character != '\n'); buffer[i-1] = '\0'; } int lenghtString (char source[]) // function to return the lenght of the string { int count = 0; while (source[count] != '\0') ++count; return count; } void insertString (char source[], char insert[], int position) //Function to insert a string inside another string from a given position { int i, j, charKeep, lenght; int lenghtString (char source[]); lenght = lenghtString (source) + lenghtString (insert); for (i = position, j = 0; i < lenght;) { if (insert[j] != '\0') { charKeep = source[i]; source[i] = insert[j]; insert[j] = charKeep; ++j; ++i; } else j = 0; } source[lenght] = '\0'; } void showString (char source[]) //Function to show a string { int i; for (i = 0; source[i] != '\0'; ++i) printf ("%c", source[i]); printf ("\n"); } int main (void) { char source[81], insert[81]; void insertString (char source[], char insert[], int position), readLine (char buffer[]), showString (char source[]); int position; printf ("Ingrese la/s palabra/s:\n"); //type in the string readLine (source); printf ("Ingrese la palabra a insertar:\n"); //type in the string to be inserted readLine (insert); printf ("Ingrese la posicion en la cual desea insertarla: "); //type the position from where the previous string will be inserted scanf ("%i", &position); insertString (source, insert, position); showString (source); return 0; }
My question is why the function insertString works fine. I thought that an array, even if I set 81 to be the size, will "compress" somehow to the lenght of the string I type in the terminal and the new size is (less than 81) cant be expanded anymore. (pd: sorry for my english. If something is not clear let me know)