Programming Kata 4
Change your working directory to your portfolio and create a new branch for this assignment.
$ cd <your-portfolio-directory>
$ git checkout -b <your-branch-name>
Now, create a subdirectory for this exercise and change your working directory to inside it.
$ mkdir exercise04
$ cd exercise04
Now create your C file and start coding!
$ vim reverse.c
Assignment
Write a program that repeatedly reads integers from standard input (one per line) and adds them to a data structure. When EOF
(end of file) is reached (fgets
returns NULL
), the program prints out the digits in reverse order. Your solution must read until EOF so that it can be used with a unix pipe (see last example below).
$ gcc reverse.c -o reverse
$ ./reverse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
$ cat nums.txt | ./reverse
273069482 234303055 -677268093 -328316162 295579978 373525782 420871987 -184718512 -985986401 261612517 -681706255 455453923 -418854480 -38792524 -36613995 376831799 -571463212 715634525 211486981 -332982468 -1066802316 -140631626 746646641 -351433281 -105403741 954165846 198727963 -61238869 -830473684 470875682 99013767 -472356179 1012464902 22947949 324553676 -593443333 285770360 -914482353 -576754080 -367698499 -393274827 430828094 -581673906 278055546 -541071135 321493305 -908397005 835261081 708695017 -721623217 -5887285 358372790 1038513940 866222620 523580581 1029576953 710897706 801594105 913489188 715624320 532152605 -997676005 276831970 532166412 810919414 165294206 1057052572 -420272965 552534298 66159651 -859766416 402411452 -101842595 170574614 112710728 -302590391 686540113 886968036 -232593458 -723419596 -686395332 201631920 908534033 -1064804836 -918416909 795728301 -957654059 1069015211 -804300323 837423370 -634949473 1073728018 -734853595 -962204059 548855665 157450556 686501732 -82848902 -634248372 -923618977 -905739578 43400795 -784041100 -872994027 557776326 299484517 1003744892 336217885 499621545 639516447 -558211804 -695332320 46307006 -960936091 966591048 -913690295 526286801 -721335604 630222860 -804286517 498535142 -746487237 -548869470 181437674 -574963967 -442037256 -282042896 536378886 -250851148 396092658 934163948 1040996274 559366294 -9783792 -802997094 -852183383 873604796 -709513379 -179312134 121212042 -480532382 -671017537 -588181543 -113744522 -413481067 614184829 -470171331 256831494 -146128921 302968274 818324778 -756644356 902218555 966909611 -610261253 -37601028 882555716 395606271 675956763 -218105597 910468189 -129794084 770251545 337807853 190353237 980258109 400870576 -321349069 844760828 -582036420 -364348239 706953965 816205355 -128624547 -717315015 403429264 -313428073 300602220 512248541 -463226389 474491544 925156991 927358722 -133922241 -989387928 67874301 360184034 26919490 558879906 -267990977 85384682 -501081487 753594504 863735261 1010679102 -562039518 -938244542 -90834827 -1030742653 -935934961 -324499950 838018133 350527157 -660965732 -888938297 55824590 964922547 -923943508 899852501 660833375 -316843286 -465328039 840803096 -214257402 579635550 57434406 582736219 -445566812 1015276633 -13780430 52156344 295391246 -438018765 241892199 728237979 27772106 -605038688 1071432244 -840076700 -795018961 -212720293 -737276041 653214606 -779039256 -552146455 -1038736612 229713913 -769652651 466641603 291438717 893772103 971155940 28778236 -290373133 276748204 -48539461 115899598 -477225174 576018669 -353856437 -649503488 884005970 640895092 607950954 -226810937 730547560
Tips
-
“A data structure” here can mean anything: an array, a stack, a linked list, etc. However, you must implement this yourself. An array is the easiest choice.
-
You can type the EOF character with ctrl-d.
-
Your program must accept any amount of input.
-
You will probably want to be using
realloc
andatoi
. -
Download the
nums.txt
file usingwget
:$ wget http://251.systems/assets/nums.txt
Turning it in
When you are complete, commit your code and create a pull request with your exercise.
Change to the root directory of your repository:
$ cd ..
Add a line to the .gitignore
file to exclude your binary.
$ echo exercise04/reverse >> .gitignore
Add the new directory (and file) to your staged commit and verify your files are staged.
$ git add exercise04
$ git status
Commit your code and push it to GitHub!
$ git commit -m "<your commit message>"
$ git push -u origin <your-branch>
There will be a link to create a pull request in the upload message. Click the link and create a PR. Add me as reviewer.