frame.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main (int argc, char** argv) {
  4. if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "?")) {
  5. printf ("List Framer\n\n");
  6. printf ("This program takes a list and builds a frame around it.\n\n");
  7. printf ("Usage:\n");
  8. printf ("\t~> %s [elements]\n", argv[0]);
  9. printf ("where [elements] is a space-delimeted list of elements to print.\n\n");
  10. printf ("For example:\n");
  11. printf ("\t~> %s Hello, world!\n", argv[0]);
  12. printf ("would return:\n");
  13. printf ("**********\n");
  14. printf ("* Hello, *\n");
  15. printf ("* world! *\n");
  16. printf ("**********\n");
  17. return 0;
  18. } else {
  19. int c, i;
  20. int len = argc - 1;
  21. int max = 0;
  22. char* list[len];
  23. for (c = 0; c < len; ++c) {
  24. list[c] = argv[c + 1];
  25. }
  26. for (c = 0; c < len; ++c) {
  27. if (strlen(list[c]) > max) {
  28. max = strlen(list[c]);
  29. }
  30. }
  31. max += 4;
  32. for (c = 0; c < max; ++c) {
  33. printf ("*");
  34. }
  35. printf ("\n");
  36. for (c = 0; c < len; ++c) {
  37. printf ("* ");
  38. printf ("%s", list[c]);
  39. if (strlen(list[c]) < max - 4) {
  40. for (i = 0; i < max - strlen(list[c]) - 4; ++i) {
  41. printf (" ");
  42. }
  43. }
  44. printf (" *\n");
  45. }
  46. for (c = 0; c < max; ++c) {
  47. printf ("*");
  48. }
  49. printf ("\n");
  50. }
  51. }