Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Created December 25, 2012 20:44
Show Gist options
  • Select an option

  • Save alexesDev/4375310 to your computer and use it in GitHub Desktop.

Select an option

Save alexesDev/4375310 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define TEST(a, b, mustBe) \
if(mustBe != getWordLength(a, b)) { printf("Error > "); } \
printf("\"%s\" word with index %d has length %d, must be %d\n", a, b, getWordLength(a, b), mustBe);
//Функция определяет длину слова с заданным номером
//0 если слово не нашлось
int getWordLength(char* sentence, int index)
{
int i = 0, j = 0;
int startWord = 0;
for (; ; ++i)
{
if(sentence[i] == ' ' || sentence[i] == '\0')
{
if(index == j)
return i - startWord;
startWord = i + 1;
++j;
}
if(sentence[i] == '\0')
return 0;
}
return 0;
}
int main(void)
{
TEST("I am Varya", 1, 2);
TEST("I am best of the best", 4, 3);
TEST("I am best of the best", 15, 0);
TEST("I am best of the best", 5, 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment