// some examples using an array of strings #include #include #include void printstrs(char [][51], int); void readstring(FILE *, char [], int); int main(int argc, char** argv) { int i; // array of 5 strings which can hold 50 char each char strings[5][51]; FILE *infile = fopen("strings.txt", "r"); // call function to read one string from the file // if you use one subscript, you are using an array of characters (a string) for (i = 0 ; i < 5 ; i++) readstring(infile, strings[i], 51); printf("\nthe strings are: \n"); for (i = 0 ; i < 5 ; i++) printf("%s\n", strings[i]); printf("\n"); // copy a string and then change some of the characters // if you use two subscripts, you are referencing an individual character // within one of the strings, so strings[3][0] is the 1st character // in the 4th string strcpy(strings[3], strings[1]); strings[3][0] = 'X'; strings[3][1] = 'Y'; strings[3][2] = 'Z'; // pass the whole array to a function that will print all the strings printf("now the strings are:\n"); printstrs(strings, 5); return (EXIT_SUCCESS); } // first parm is an array of strings, each string has 51 locations // second parm is the number of strings to print void printstrs(char strarr[][51], int numstr) { int i; for (i = 0 ; i < numstr ; i++) printf("%s\n", strarr[i]); } // read a string from the given input file void readstring(FILE *infile, char str[], int len) { fgets(str, len, infile); }