100 likes | 186 Views
EEE 243B Applied Computer Programming. Strings. Example #1. Write code to take a first name and last name and create a new string with the full name first is “Daffy”, last is “Duck” fullName is “Daffy Duck”. Solution Example #1. #include <stdio.h> #include <string.h> int main(void) {
E N D
Example #1 • Write code to take a first name and last name and create a new string with the full name • first is “Daffy”, last is “Duck” • fullName is “Daffy Duck”
Solution Example #1 #include <stdio.h> #include <string.h> int main(void) { char firstName[15] = "Daffy"; char lastName[15] = "Duck"; char fullName[20]; strcpy(fullName,firstName); strcat(fullName, lastName); printf("The full name is %s\n",fullName); return(0); }
Example 2 • What is the output from the following code? char player[15]; strcat(player,"Wayne"); strcat(player,"Gretzky"); printf("Player#1: %s\n",player); strcat(player,"Mark"); strcat(player,"Messier"); printf("Player#2: %s\n",player);
Solution Example 2 • May have unexpected results since player is not initialized • If char player[15]=""; Player1: WayneGretzky Player2: WayneGretzyMarkMessier (OR may crash the system since has more than 15 characters)
Example 3 • Consider the 2-D array of names • Write the code to find • the first name in alphabetical order • the last name in alphabetical order • Hint: Use the strcmp function char greatOilers[7][15] = { "Gretzky", "Messier", "Smith", "Coffey", "Smyth", "Kurri", "Fuhr" };
Solution Example 3 #include <stdio.h> #include <string.h> int main(void) { char greatOilers[7][15] = { "Gretzky","Messier", "Smith", "Coffey", "Smyth", "Kurri", "Fuhr" }; char first[15]; char last[15]; int cnt =0, numPlayers=7; strcpy(start,greatOilers[0];) strcpy(last,greatOilers[0];) for (cnt=1; cnt<numPlayers; cnt++) { if (strcmp(start,greatOilers[cnt] < 0) strcpy(first, greatOilers[cnt]); if (strcmp(last,greatOilers[cnt] > 0) strcpy(last, greatOilers[cnt]); } printf("In Alphabetical Order: First: %s Last: %s\n", first, last); return(0); }
Summary • 'C' strings are allocated to have a fixed maximum length • Be careful when using strcat • Use strcpy when appropriate • Initialize empty strings to \0 ("") • Many more useful string function • strlen, strncpy, strncat, strcasecmp, strcoll • See glib manual on website
Solution 2 • #include <stdio.h> • #include <string.h> • int main(void) • { • char mystr1[] = "What happens to this memory"; • char player[16]; • char mystr2[] = "Is this memory corrupted"; • // strcat(player,"Wayne"); • strcpy(player,"Wayne"); • strcat(player,"Gretzky"); • printf("Player#1: %s\n",player); • printf("mystr1 = %s\n\n",mystr1); • printf("mystr2 = %s\n\n",mystr2); • strcat(player,"Mark"); • strcat(player,"Messier"); • printf("Player#2: %s\n",player); • printf("mystr1 = %s\n\n",mystr1); • printf("mystr2 = %s\n\n",mystr2); • printf("Addresses: %i %i %i\n", (int) mystr1, (int) player, (int) mystr2); • getch(); • return(0); • }
Output and Explanation • Overwrites memory allocated before the string • String and addresses show below