Download presentation
Presentation is loading. Please wait.
Published byCharles Hayre Modified over 9 years ago
1
16.216 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5
2
Lecture outline Announcements/reminders Program 7 due 4/20 Program 8 to be posted by end of week Grading on previous programs to be completed ASAP Today Review strings and character arrays PE5 Can download “starter” code from website http://mgeiger.eng.uml.edu/16216/sp12/PE_code/index.htm#PE5 4/17/2015 ECE Application Programming: Lecture 31 2
3
Review: strings Represented as character arrays Can be initialized using string constants char hello[] = “Hello”; Can access individual elements hello[3] = ‘l’; Can print directly or with formatting Print directly: printf(hello); Print w/formatting using %s: printf(“%s\n”, hello); Must leave enough room for terminating ‘\0’ 4/17/2015 ECE Application Programming: Lecture 31 3
4
Review: String functions In library: Copying strings: char *strcpy(char *dest, const char *source); char *strncpy(char *dest, const char *source, size_t num); Return dest Comparing strings: int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t num); Character-by-character comparison of character values Returns 0 if s1 == s2, 1 if s1 > s2, -1 if s1 < s2 4/17/2015 ECE Application Programming: Lecture 31 4
5
Review: String functions (cont.) Find # of characters in a string size_t strlen(const char *s1); Returns # characters before ‘\0’ Not necessarily size of array “Add” strings together—string concatenation char *strcat(char *dest, const char *source); char *strncat(char *dest, const char *source, size_t num); Returns dest 4/17/2015 ECE Application Programming: Lecture 31 5
6
Example: Strings What does the following program print? int main() { char s1[15]; int n1; char s2[10] = “.216”; int n; strncpy(s1, “16”, 15); n1 = strlen(s1); printf(“s1 = %s\n”, s1); printf(“Length of s1 = %d\n\n”, n1); printf(“%c\n\n”, s1[1]); strncat(s1,s2,10); n1 = strlen(s1); printf(“s1 = %s\n”, s1); printf(“Length of s1 = %d\n\n”, n1); // Assume user inputs: ABC ABD printf(“Enter two strings:”); scanf(“%s%s”, s1, s2); n = strncmp(s1, s2, 15); if (n > 0) printf(“%s > %s\n”, s1, s2); else if (n < 0) printf(“%s < %s\n”, s1, s2); else printf(“%s == %s\n”, s1, s2); return 0; } 4/17/2015 ECE Application Programming: Lecture 31 6
7
Example solution s1 = 16 Initial value of s1 Length of s1 = 2 6 s1[1] s1 = 16.216 s1 after strncat() Length of s1 = 6 Enter two strings: ABC ABD ABC < ABD Result of strncmp() 4/17/2015 ECE Application Programming: Lecture 31 7
8
Example: Using string functions Works with main program in PE Assume input strings have max of 49 chars (+ ‘\0’) Write a function to do each of the following: int readStrings(char *s); Repeatedly read strings from standard input until the input string matches s. Return the number of strings read. void copyNull(char *s1, char *s2, int n); Copy the first n characters of s2 into s1, and make sure that the new version of s1 terminates with a null character. int fillString(char *s, int n); Repeatedly read strings from standard input and concatenate them to s until there is no room in the string. Return the final length of the string. For example, if s is a 6-character array already holding “abcd”: User enters “e”—string is full; return 5 User enters “ef”—there’s not enough room; return 4 Assume s initially contains null terminated string (or is empty) 4/17/2015 ECE Application Programming: Lecture 31 8
9
Example solution int readStrings(char *s) { char str[50];// Assume max 50 chars // (not specified) int count = 0; do { scanf(“%s”, str);// NOTE: do not // need &str count++; } while (strcmp(str, s) != 0); return count; } 4/17/2015 ECE Application Programming: Lecture 31 9
10
Example solution (cont.) void copyNull(char *s1, char *s2, int n) { strncpy(s1, s2, n); s1[n] = ‘\0’; } 4/17/2015 ECE Application Programming: Lecture 31 10
11
Example solution (cont.) int fillString(char *s, int n) { char input[50];// Assume max // 50 chars int charsLeft;// Space remaining // in s do { scanf(“%s”, input); // Calculate # chars left in array if input // string is added. Need to leave room for ‘\0’ charsLeft = n – (strlen(s) – 1) – strlen(input); if (charsLeft > 0)// Enough space to add this string strcat(s, input);// and continue else {// Out of room if (charsLeft == 0)// Can add input, but then out of room strcat(s, input); return strlen(s); } } while (1); } 4/17/2015 ECE Application Programming: Lecture 31 11
12
Next time 2-dimensional arrays May also start file I/O 4/17/2015 ECE Application Programming: Lecture 31 12
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.