// chapter6_6 with added strupper() function and %zd formats for strlen // /*------------------------------------------------------------*/ /* Program chapter6_6 */ /* */ /* This program illustrates the use of string functions. */ #include #include #include void strupper( char s[]){ // for( int i = 0; i < strlen(s); ++i) // worst, slowest, calls strlen every time through the loop // int n = strlen(s); // for( int i = 0; i < n; ++i) // better, call strlen once before the loop for( int i = 0; s[i] != 0; ++i) // best, fastest, no call to strlen, just stop at the zero byte s[i] = toupper(s[i]); } int main(void) { /* Declare and initialize variables. */ char strg1[]="Engineering Problem Solving: "; char strg2[]="Fundamental Concepts", strg3[50]; /* Print the length of strings. */ printf("String lengths: %zd %zd \n", strlen(strg1),strlen(strg2)); /* Combine two strings into one. */ strcpy(strg3,strg1); strcat(strg3,strg2); printf("strg3: %s \n",strg3); strupper(strg3); printf( "%s\n", strg3); printf("strg3 length: %zd \n",strlen(strg3)); /* Exit program. */ return 0; } /*---------------------------------------------------------*/ /* output: String lengths: 29 20 strg3: Engineering Problem Solving: Fundamental Concepts ENGINEERING PROBLEM SOLVING: FUNDAMENTAL CONCEPTS strg3 length: 49 */