1 00:00:00,000 --> 00:00:00,366 2 00:00:00,366 --> 00:00:01,830 >> SPEAKER: We'll call it a string. 3 00:00:01,830 --> 00:00:03,510 It's just a sequence of characters. 4 00:00:03,510 --> 00:00:05,790 Indeed, it's simply an array of characters. 5 00:00:05,790 --> 00:00:09,730 And so even if we get a string from the user in the usual way with CS50's 6 00:00:09,730 --> 00:00:13,550 GetString, we can then proceed to iterate over the chars in that string 7 00:00:13,550 --> 00:00:17,110 one at a time as though that string is indeed an array. 8 00:00:17,110 --> 00:00:18,660 Let's try this in code. 9 00:00:18,660 --> 00:00:21,470 >> Include cs50.h. 10 00:00:21,470 --> 00:00:24,440 Include stdio.h. 11 00:00:24,440 --> 00:00:27,960 And let's also include string.h so that we have access 12 00:00:27,960 --> 00:00:29,500 to StringLen function. 13 00:00:29,500 --> 00:00:33,220 Let's now declare main as int main void. 14 00:00:33,220 --> 00:00:36,740 And let's now proceed to get a string from the user. 15 00:00:36,740 --> 00:00:39,480 Printf input. 16 00:00:39,480 --> 00:00:45,180 Let's now declare a string calling it s, and call our friend GetString. 17 00:00:45,180 --> 00:00:49,570 >> Let's now proceed to check, did the user indeed give me a string because 18 00:00:49,570 --> 00:00:53,370 it turns out per GetString's own documentation, GetString could on 19 00:00:53,370 --> 00:00:56,830 occasion return NULL, a special sentinel value that essentially 20 00:00:56,830 --> 00:00:59,630 indicates that the user did not cooperate and somehow did 21 00:00:59,630 --> 00:01:01,150 not provide a string. 22 00:01:01,150 --> 00:01:03,190 So let's check for that with a condition. 23 00:01:03,190 --> 00:01:09,300 >> IF s does not equal NULL, then we can assume that s is indeed a string, an 24 00:01:09,300 --> 00:01:14,580 array of characters, and proceed to iterate over those characters. 25 00:01:14,580 --> 00:01:22,240 FOR int i gets 0, let's also declare n as equal to the string length of s so 26 00:01:22,240 --> 00:01:27,900 long as i is less than n, and on each iteration, let's increment i. 27 00:01:27,900 --> 00:01:35,200 Within this loop THEN, let's call printf of %c backslash n and then plug 28 00:01:35,200 --> 00:01:41,140 into this value s bracket i thereby printing one character at a time each 29 00:01:41,140 --> 00:01:42,420 of the cars in s. 30 00:01:42,420 --> 00:01:45,210 >> Let's now compile and run this program. 31 00:01:45,210 --> 00:01:47,140 Make string. 32 00:01:47,140 --> 00:01:52,500 ./string My input will be "hello." And there we have it. 33 00:01:52,500 --> 00:01:55,410 H-E-L-L-O, each char on its own line. 34 00:01:55,410 --> 00:01:56,727