#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare(const void *a, const void *b) {
return ( *(int*)a - *(int*)b );
}
int secondSmallestWordLength(char str[]) {
char *arr = strtok(str, " ");
int count = 0;
int *lengths = NULL;
while (arr != NULL) {
count++;
lengths = realloc(lengths, count * sizeof(int));
lengths[count - 1] = strlen(arr);
arr = strtok(NULL, " ");
}
if (count < 2) {
free(lengths);
return -1;
}
qsort(lengths, count, sizeof(int), compare);
int result = lengths[1];
free(lengths);
return result;
}
int main() {
char str[] = "java c++ python c# javascript";
printf("%d\n", secondSmallestWordLength(str));
return 0;
}
/*
run:
3
*/