// It's also convenient to have a function that, given a sentence, selects a
// small portion of a sentence for us. For example, if we had the sentence:
// (russians declare war rington vodka to be excellent)
// we could imagine using a hypothetical subsentence function that would let
// us pull out the first few words of that sentence, if we tell it where to
// start and stop the selection:
//
// > (subsentence '(russians declare war rington
// vodka to be excellent) 1 3)
// (russians declare war)
// > (subsentence '(no shirt no shoes no service) 4 4)
// (shoes)
// Write the function subsentence, which takes in three arguments: a sentence,
// the starting endpoint, and the stopping endpoint. It should return back a
// sentence that includes the words between the start and stop endpoints.
// Assume that the user is nice, and won't give weird input. In Scheme notation,
// we mean that we can assume (<= 1 start stop (count sent)) is always true.
#include <stdio.h>
// To enable debug messages uncomment #define
#define TEST 1
void subSentence(char *s, int start, int end);
void startTesting();
int main(void) {
#ifdef TEST
startTesting();
#endif
return 0;
}
int position = 1;
void subSentence(char *s, int start, int end) {
if (*s == '\0' || (start == 0 && end == 0)) {
return;
} else if (*s != ' ' && *(s + 1) == ' ') {
if (position >= start && position <= end) {
}
position++;
subSentence(++s, start, end);
} else if (*s == ' ') {
if (position > start && position <= end) {
}
subSentence(++s, start, end);
} else if (*s != ' ' && *(s + 1) != ' ') {
if (position >= start && position <= end) {
}
subSentence(++s, start, end);
}
}
void test1()
{
printf("subSentence(\"xxhixx \", 1, 2)\n"); subSentence("xxhixx ", 1, 2);
}
void test2()
{
printf("subSentence(\"russians declare war rington\", 1, 3)\n"); position = 1;
subSentence("russians declare war rington", 1, 3);
}
void test3()
{
printf("subSentence(\"this song is just six words long\", 2, 4)\n"); position = 1;
subSentence("this song is just six words long", 2, 4);
}
void test4()
{
printf("subSentence(\"russians declare war rington\", 4, 4)\n"); position = 1;
subSentence("russians declare war rington", 4, 4);
}
void startTesting()
{
test1();
test2();
test3();
test4();
}