Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger Fall 2018 Lecture 24 Character arrays and strings (continued)

2 ECE Application Programming: Lecture 24
Lecture outline Announcements/reminders Program 5 due 11/7 Exam 2 in class Monday, 11/5 Will be allowed one 8.5” x 11” note sheet Will cover lectures (except lecture 16) Lec. 25: Exam 2 Preview (Fri. 11/2) Today’s lecture Review: String basics String functions 7/3/2019 ECE Application Programming: Lecture 24

3 ECE Application Programming: Exam 2 Preview
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); Reading strings: scanf(“%s”, str); Reads all characters up to (but not including) first space, tab, or newline Must leave enough room for terminating ‘\0’ 7/3/2019 ECE Application Programming: Exam 2 Preview

4 ECE Application Programming: Lecture 24
String functions Things we’d like to do with strings: Set one equal to another Compare two strings Find # characters in string String may not fill array (“buffer”) allocated for it “Add” two strings together “abc” + “def” = “abcdef” 7/3/2019 ECE Application Programming: Lecture 24

5 String functions (cont.)
In <string.h> library: Copying strings: char *strcpy(char *dest, const char *source); char *strncpy(char *dest, const char *source, size_t num); Return dest Does not append ‘\0’ unless length of source < num 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, >0 if s1 > s2, <0 if s1 < s2 7/3/2019 ECE Application Programming: Lecture 24

6 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 7/3/2019 ECE Application Programming: Lecture 24

7 ECE Application Programming: Lecture 24
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; } 7/3/2019 ECE Application Programming: Lecture 24

8 ECE Application Programming: Lecture 24
Example solution s1 = 16 Initial value of s1 Length of s1 = 2 6 s1[1] s1 = s1 after strncat() Length of s1 = 6 Enter two strings: ABC ABD ABC < ABD Result of strncmp() 7/3/2019 ECE Application Programming: Lecture 24

9 ECE Application Programming: Lecture 24
Final notes Next time Exam 2 Preview Reminders: Program 5 due 11/7 Exam 2 in class Monday, 11/5 Will be allowed one 8.5” x 11” note sheet Will cover lectures (except lecture 16) Lec. 25: Exam 2 Preview (Fri. 11/2) 7/3/2019 ECE Application Programming: Lecture 24


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google