1 ICS103 Programming in C Lecture 17: Array of Strings.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Chubaka Producciones Presenta :.
Nested Loops. Problem Print The only printfs you can use are: –printf(“*”); –printf(“\n”); *****
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
Chapter 6 Control Structures.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Chicas, este calendario si es pa' nosotras !!!!!.
6/23/11 FAIS,LLC Five Apple/Inpatient Specialist Case Study Patient Volume/LOS/ Payor Source Data.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.

You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
1 ICS103 Programming in C Ch 8: Strings. O UTLINE What is a String? The NULL Character ‘\0’ in Strings Input/Output with printf and scanf Input/Output.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
6/23/11 FAIS, Ind PC Five Apple/Inpatient Specialist Case Study Patient Volume/LOS/ Payor Source Data.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Calendar for 2011 Months of the Year with Holidays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
TEMPORAL VISUALIZATION OF DATA FROM THE FRENCH SENTINEL NETWORK.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Chapter 7 – Arrays.
CS1010 Programming Methodology
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
TIMELINES PHOTOS This is an example text
TIMELINES PHOTOS This is an example text
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Problem Gambling Clicks to Opgr.org
2300 (11PM) September 21 Blue line is meridian..
Lec 11.
McDonald’s calendar 2007.
1 - January - Sun Mon The Wed Thu Fri Sat
Teacher name August phone: Enter text here.
February 2007 Note: Source:.
Character Arrays char string1[] = “first”;
MONTHS OF THE YEAR January February April March June May July August
McDonald’s calendar 2007.
Production Month Sun Hours K Monthly Kwh Tou Peak Value After Kwh
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Introduction to Problem Solving and Programming
Presentation transcript:

1 ICS103 Programming in C Lecture 17: Array of Strings

2 Outline Array of Strings (2D character array)  Declaration and Initialization  Input/Output with Arrays of Strings  Examples

3 Array of Strings To represent arrays of strings we need 2-dimensional arrays of characters. The first-dimension represents the number of strings in the array and the second-dimension represents the maximum string length for each string (including the terminating ‘\0’). The following are statements to declare an array to store up to 30 names, each of maximum length 25 characters. #define NUM_NAMES 30 #define NAME_LEN char names[NUM_NAMES][NAME_LEN];

4 Array of Strings (cont’d) We can also initialize an array of strings at declaration in the following manner: char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; Note: month[7][2] is the character ‘g’, and month[4] is the string “May”) including the terminating ‘\0’)

5 Input/Output with Arrays of Strings The easiest way to read an array of strings is to use the gets function. Similarly, the easiest way to print an array of strings is to use the puts function. The following example scans five sentences from the user and prints them. #include #define NUM_LINES 5 #define LINE_LEN81 int main(void) { char lines[NUM_LINES][LINE_LEN]; int i; printf("Please entert 5 sentences (not exceeding 80 characters)\n"); for ( i = 0 ; i < NUM_LINES; ++i ) { printf ("Enter the sentence number %d: ", i+1 ) ; gets (lines[i]) ; } printf("\nYou typed: \n"); for (i = 0; i< NUM_LINES; i++) puts (lines[i]); system("pause"); return 0; } Note that there is no need to use nested loops to process an array of strings

6 Example /* This program creates an array of strings and performs various string operations on them*/ #include #define NUM 10 #define LEN 20 int main(void ){ int i = 0, found = 0; char str[LEN]; char names [NUM][LEN] = {"Abdullah", "Ibrahim", "Husam", "Ahmad", "Hamzah", "Abdul-aziz", "Abdul-majeed", "Nayef", "Akhaled", "Yousef"}; printf("The length of the names are:\n"); for(i = 0; i < NUM; i++){ printf("%-4d%-12s of length %d\n", i+1,names[i], strlen(names[i])); } printf("\nEnter a name to search in the list: "); gets(str); i = 0; while (!found && i<NUM) { if(strcmp(names[i], str) == 0) found = 1; else i++; }

7 Example … if(found) printf("%s is in the list at row %d\n",str,i); else printf("%s is not in the list.\n", str); system("pause"); return 0; }

8 Use of break in loops We saw the use of break to exit from a switch statement. Another use of break is to terminate a loop before it reaches its terminating condition. For example, instead of using the flag, found, as an additional condition for terminating the searching loop in the last example, we could simply break to terminate the loop as follows. while (i<NUM) { if(strcmp(names[i], str) == 0) break; else i++; } We can also use break to terminate a for or do-while loop as shown in the next example.

9 Example #include #define NUM 10 #define LEN 20 int main(void ) { int i = 0; char str[LEN]; char names [NUM][LEN] = {"Abdullah", "Ibrahim", "Husam", "Ahmad", "Hamzah", "Abdul-aziz", "Abdul-majeed", "Nayef", "Akhaled", "Yousef"}; printf("The length of the names are:\n"); for(i = 0; i < NUM; i++){ printf("%-4d%-12s of length %d\n", i+1,names[i], strlen(names[i])); } printf("\nEnter a name to search in the list: "); gets(str); for (i=0; i<NUM; i++) { if(strcmp(names[i], str) == 0) break; } if (i == NUM) //the loop terminated because the array is exhausted printf("%s is not in the list.\n", str); else printf("%s is in the list at row %d\n",str,i); system("pause"); return 0; }

10 Use of continue in Loops continue is another useful construct that allows you break the current iteration of a loop before it finishes and start another one. The next example demonstrate how it works. #include int main(void) { int i; for (i=0; i<10; i++) { printf("\nMa'assalama "); if (i < 5) continue; printf("Shabab"); } printf("\n"); system("pause"); return 0; }