1 / 10

EEE 243B Applied Computer Programming

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) {

hateya
Download Presentation

EEE 243B Applied Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEE 243BApplied Computer Programming Strings

  2. 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”

  3. 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); }

  4. 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);

  5. 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)

  6. 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" };

  7. 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); }

  8. 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

  9. 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); • }

  10. Output and Explanation • Overwrites memory allocated before the string • String and addresses show below

More Related