If the original entered string should remain unchanged, then use the standard C functions strcspn and strspn
Below is a demonstration program that is based on a solution with the specified functions.
#include <stdio.h> #include <string.h> #define N 255 int main( void ) { const size_t M = 2; while ( 1 ) { char s[N]; printf( "Enter a sentence: (Enter - exit): " ); if ( fgets( s, N, stdin ) == NULL || s[0] == '\n' ) break; s[ strcspn( s, "\n" ) ] = '\0'; printf( "\n" ); for ( const char *first = s; *first; ) { const char *delimiter = " \t"; size_t m = strspn( first, delimiter ); first += m; m = strcspn( first, delimiter ); if ( M < m ) printf( "%*.*s ", m, m, first ); first += m; } printf( "\n" ); } return 0; }
The output of the program to the console may look as follows.
Enter a sentence: (Enter - exit): Hello, world! Hi Hello, world! Enter a sentence: (Enter - exit):
That is, the first query introduced the string Hello, world! Hi Hello, world! Hi , and during the second query, the Enter key was simply pressed without entering data to stop the loop.
You can change the program so that the value of the minimum word length is also entered by the user through the console.