count = 0; posmin = 0; countmin = strlen(str); for(i = 0; i <= strlen(str); i++){ if(isalpha(str[i]) == 1){ if(isalpha(str[i + 1]) == 0){ count++; if(count < countmin){ countmin = count; posmin = i; count = 0; } } else count++; } } 
  • @Sergej SadVlad Format the code to make it look normal. He has "floated away" somewhere to the right. - Vlad from Moscow
  • @Sergej SadVlad And it is not clear whether to find the minimum word, or to find the minimum word length in the string. What exactly should be found and what should be the result? - Vlad from Moscow
  • @VladfromMoscow find the word min means we have to find mines along the length - Sergei
  • Why not break a string into words using strtok ? - PinkTux
  • @Sergei Still the minimum word or the minimum word length? And what should be done as a result, when will it be found ?! - Vlad from Moscow

3 answers 3

If it strtok impossible strtok , then, for example, so (assuming that the word is only from the isalpha symbols):

 int main() { char * str = " cbvahv xchnvz jh jhs sdfjhgbvjhsbvdf sdfh "; int count = 0, posmin = -1, countmin = strlen(str); for(int i = 0; i < strlen(str);) { while(str[i] && !isalpha(str[i])) ++i; if (str[i] == 0) break; int pos = i; while(str[i] && isalpha(str[i])) ++i; count = i - pos; if (count < countmin) { countmin = count; posmin = pos; } } for(int i = posmin; i < posmin+countmin; ++i) putchar(str[i]); putchar('\n'); printf("Len = %d, pos = %d\n",countmin,posmin); } 
  • Thank you very much @Harry! But the expression while (str [i]) means "as long as the element belongs to the string"? - Sergei
  • Simply - until the line has ended (not a null terminator). If you are satisfied with the answer - mark it as accepted :) - Harry

As an option:

 #include <stdio.h> #include <string.h> #include <limits.h> /* * Немного другой подход: определяем слово по границам, * а не по набору символов */ #define DELIMITERS " \n\r\t,.;!?:-()[]<>{}\"'|+-" int main( void ) { const char text[] = "678 s 1234 56"; const char *s = text, *minptr = text; size_t minlen = strlen( text ); while( *s ) { const char *ptr; while( *s && strchr( DELIMITERS, ( *s & CHAR_MAX ) ) ) { s++; } ptr = s; while( *s && !strchr( DELIMITERS, ( *s & CHAR_MAX ) ) ) { s++; } if( *ptr && s - ptr < minlen ) { minlen = s - ptr; minptr = ptr; } } /* minptr - указатель на начало слова, если нужно * вычленить само слово, то это уже отдельно */ printf( "%zu - %s\n", minlen, minptr ); return 0; } 

    Finding the minimum word in a line involves finding its two characteristics: it is a pointer to the beginning of a word in a line and the length of the word itself.

    To store these characteristics, it is best to create a structure of two data members and use a separate function that will find the minimum word for any string.

    Below is a demo program that shows how to do it.

     #include <stdio.h> #include <ctype.h> struct Position { char *p; size_t n; }; Position min_word(const char *s) { Position min = { NULL, 0 }; while (*s) { while (isblank((unsigned char)*s)) ++s; if (*s) { Position current = { ( char * )s, 0 }; while (*s && !isblank((unsigned char)*s)) { ++current.n; ++s; } if (min.p == NULL || current.n < min.n) min = current; } } return min; } int main(void) { char s[] = "Hello hi chiao"; Position min = min_word(s); printf("The minimum word in the string is %*.*s with length of %zu\n", min.n, min.n, min.p, min.n); return 0; } 

    Output of the program to the console

     The minimum word in the string is hi with length of 2 

    Note that finding the minimum word in a line does not require using string functions, such as strlen , as this is simply a waste of time.