1 00:00:00,000 --> 00:00:03,250 >> SPEAKER 1: Let's now write a program they truly compares two strings 2 00:00:03,250 --> 00:00:04,245 character for character. 3 00:00:04,245 --> 00:00:06,830 Let's use compare 0 as our starting point. 4 00:00:06,830 --> 00:00:11,550 But let's first peel back the layer that is the string type and rewrite it 5 00:00:11,550 --> 00:00:14,120 for what it truly is, which is a char star. 6 00:00:14,120 --> 00:00:17,740 That is the address of a character, specifically the first character in a 7 00:00:17,740 --> 00:00:21,010 sequence of characters that we'd more generally know as a string. 8 00:00:21,010 --> 00:00:21,880 >> Same for t. 9 00:00:21,880 --> 00:00:25,660 Let's rewrite that string declaration as char star. 10 00:00:25,660 --> 00:00:28,690 And now we need to no longer compare s against t, lest 11 00:00:28,690 --> 00:00:30,150 we compare two addresses. 12 00:00:30,150 --> 00:00:33,180 We want to truly compare the two strings themselves. 13 00:00:33,180 --> 00:00:37,520 To do this, we can use a function declared in string.h. 14 00:00:37,520 --> 00:00:40,920 So I'll add that include on top of my file. 15 00:00:40,920 --> 00:00:43,130 >> And then I'm going to change this line here. 16 00:00:43,130 --> 00:00:47,920 Rather than compare s against t, I'm going to call a function stir comp, 17 00:00:47,920 --> 00:00:52,290 for string compare, and pass in as arguments s and t. 18 00:00:52,290 --> 00:00:56,480 We'll defer then to string compare to figure out if s and t are indeed equal 19 00:00:56,480 --> 00:00:59,870 and let it figure out how to compare them character for character. 20 00:00:59,870 --> 00:01:02,410 >> Now, according to the documentation for string compare, it's actually 21 00:01:02,410 --> 00:01:06,920 going to return 0 if the two strings pointed at by s and t are the same. 22 00:01:06,920 --> 00:01:09,490 It's going to return a negative number if s should come before t 23 00:01:09,490 --> 00:01:13,740 alphabetically or a positive number if s should come after t alphabetically. 24 00:01:13,740 --> 00:01:16,090 >> But for now, we only care about equality. 25 00:01:16,090 --> 00:01:19,270 So I'm going to simply test if the return value of string compare, 26 00:01:19,270 --> 00:01:21,450 passing in s and t, equals 0. 27 00:01:21,450 --> 00:01:24,940 And if so, I'm going to claim that the two strings are the same. 28 00:01:24,940 --> 00:01:26,820 >> But I'm going to make one other change as well. 29 00:01:26,820 --> 00:01:30,410 It turns out that get string, per its documentation, can sometimes return 30 00:01:30,410 --> 00:01:34,320 null, a sentinel value that, according to get strings documentation, means 31 00:01:34,320 --> 00:01:35,450 something bad happened. 32 00:01:35,450 --> 00:01:38,830 For instance, we ran out of memory or the user somehow didn't cooperate. 33 00:01:38,830 --> 00:01:41,080 >> String compare, meanwhile, is a little fragile. 34 00:01:41,080 --> 00:01:44,730 If you pass it null for either its first or its second argument, bad 35 00:01:44,730 --> 00:01:45,650 things can happen. 36 00:01:45,650 --> 00:01:47,970 Bad things usually involving segmentation faults. 37 00:01:47,970 --> 00:01:52,210 So to avoid that potential altogether, I'm first going to wrap this use of 38 00:01:52,210 --> 00:01:56,350 string compare by indenting this whole block of code and first only doing 39 00:01:56,350 --> 00:02:03,140 that if s is not equal to null and t is not equal to null. 40 00:02:03,140 --> 00:02:08,280 >> Wrapping that if else construct that I typed earlier with curly braces as 41 00:02:08,280 --> 00:02:12,270 well, so that this time I only touch s and t if I'm certain that 42 00:02:12,270 --> 00:02:13,450 they are not null. 43 00:02:13,450 --> 00:02:17,220 Let's now save, compile, and re-run this program. 44 00:02:17,220 --> 00:02:22,240 >> Make compare 1 dot slash compare 1. 45 00:02:22,240 --> 00:02:23,950 I'll say hello again. 46 00:02:23,950 --> 00:02:25,890 Followed by hello yet again. 47 00:02:25,890 --> 00:02:28,110 And this time, I indeed type the same thing. 48 00:02:28,110 --> 00:02:30,255