/* * Write a function that takes a string * argument and capitalizes the letters in it. * Do this two ways: * 1. array subscripts * 2. pointer arithmetic * */ #include #include // modify string[] in place void to_upper_1(char string[]); // same, but use pointers void to_upper_2(char* string); int main(){ // The two possible string initialzations // are *not* equivalent. // This is a character array, which is modifiable. char string1[] = "This is a 123 string."; // This is a string constant, which is not. //char* string2 = "So is 456 this."; char string2[] = "So is 456 this."; // This is the same sort of thing, too. int array[] = {10, 11, 12, 13, 14}; // However, those notations work *only* // for initial declarations, not in the code. // So later 'string1="Hello"' is not legit. printf("string1 is '%s'\n", string1); printf(" calling to_upper_1(string1) \n"); to_upper_1(string1); printf("string1 is '%s'\n", string1); printf("\n"); printf("string2 is '%s'\n", string2); printf(" calling to_upper_2(string2) \n"); to_upper_2(string2); printf("string2 is '%s'\n", string2); printf("\n"); return 0; } void to_upper_1(char string[]){ //printf(" ... in to_upper_1 ... \n"); int i; for (i=0; i= 'a' && string[i] <= 'z'){ string[i] += ('A' - 'a'); } } //printf("\n"); } void to_upper_2(char* string){ // This is Jim's version of 'the evil loop' while (*string){ // printf(" at %p : '%c', \n", string, *string); if (*string >= 'a' && *string <= 'z'){ *string += ('A' - 'a'); } string++; } }