• Source
    1. // It's also convenient to have a function that, given a sentence, selects a
    2. // small portion of a sentence for us. For example, if we had the sentence:
    3. // (russians declare war rington vodka to be excellent)
    4. // we could imagine using a hypothetical subsentence function that would let
    5. // us pull out the first few words of that sentence, if we tell it where to
    6. // start and stop the selection:
    7. //
    8. // > (subsentence '(russians declare war rington
    9. // vodka to be excellent) 1 3)
    10. // (russians declare war)
    11. // > (subsentence '(no shirt no shoes no service) 4 4)
    12. // (shoes)
    13. // Write the function subsentence, which takes in three arguments: a sentence,
    14. // the starting endpoint, and the stopping endpoint. It should return back a
    15. // sentence that includes the words between the start and stop endpoints.
    16. // Assume that the user is nice, and won't give weird input. In Scheme notation,
    17. // we mean that we can assume (<= 1 start stop (count sent)) is always true.
    18.  
    19.  
    20. #include <stdio.h>
    21.  
    22. // To enable debug messages uncomment #define
    23. #define TEST 1
    24.  
    25. void subSentence(char *s, int start, int end);
    26. void startTesting();
    27.  
    28. int main(void) {
    29. #ifdef TEST
    30. startTesting();
    31. #endif
    32.  
    33. return 0;
    34. }
    35.  
    36. int position = 1;
    37. void subSentence(char *s, int start, int end) {
    38. if (*s == '\0' || (start == 0 && end == 0)) {
    39. return;
    40. } else if (*s != ' ' && *(s + 1) == ' ') {
    41. if (position >= start && position <= end) {
    42. printf("%c", *s);
    43. }
    44. position++;
    45.  
    46. subSentence(++s, start, end);
    47. } else if (*s == ' ') {
    48. if (position > start && position <= end) {
    49. printf("%c", *s);
    50. }
    51.  
    52. subSentence(++s, start, end);
    53. } else if (*s != ' ' && *(s + 1) != ' ') {
    54. if (position >= start && position <= end) {
    55. printf("%c", *s);
    56. }
    57.  
    58. subSentence(++s, start, end);
    59. }
    60. }
    61.  
    62. void test1()
    63. {
    64. printf("subSentence(\"xxhixx \", 1, 2)\n");
    65. subSentence("xxhixx ", 1, 2);
    66. printf("\n\n");
    67. }
    68.  
    69. void test2()
    70. {
    71. printf("subSentence(\"russians declare war rington\", 1, 3)\n");
    72. position = 1;
    73. subSentence("russians declare war rington", 1, 3);
    74. printf("\n\n");
    75. }
    76.  
    77. void test3()
    78. {
    79. printf("subSentence(\"this song is just six words long\", 2, 4)\n");
    80. position = 1;
    81. subSentence("this song is just six words long", 2, 4);
    82. printf("\n\n");
    83. }
    84.  
    85. void test4()
    86. {
    87. printf("subSentence(\"russians declare war rington\", 4, 4)\n");
    88. position = 1;
    89. subSentence("russians declare war rington", 4, 4);
    90. printf("\n\n");
    91. }
    92.  
    93. void startTesting()
    94. {
    95. test1();
    96. test2();
    97. test3();
    98. test4();
    99. }
    100.