There is a string, for example, "A simple example of a string to test the operation of an algorithm, a survey."

There is such a combination of "pro"

At the output you need to get the following words: "Simple poll check" Part of the algorithm must be made recursive.

As I understand it - you first need to break the line into words, replacing the spaces '\ 0'. It is necessary to somehow get pointers to words - but then I had a hitch.

Next you need to create an array of possible combinations, i.e. "about rpg opr orp ro pore".

And at the end, compare each word from an array of combinations of characters to a string, but this is not very effective.

  • A "check" is not necessary? Why? What is the general rule that you need to pull out? - Harry
  • need to get the words in which there is a given combination. - Hardc0re
  • Then why is the "check" not selected? And - you need any options? Say, "poll" - because of "ODA" or because of "about"? What about aparao? - Harry
  • Forgot add. No, you need only in those versions where these letters stand together "poll" because of "ODA". "aparao" is not necessary. - Hardc0re
  • A "orpos" fit? - PinkTux

1 answer 1

Well, here is a blank. Recursively here you can drive set by word (or vice versa):

 #include <string.h> static const char DELIMITERS[] = " \n\r\t"; static size_t find_set( const char *src, const char *set ) { size_t words = 0; char *dup = strdup( src ); if( dup ) { char *word = strtok( dup, DELIMITERS ); while( word ) { /* * а здесь проверяем есть ли в word искомое, * есть - printf( "%zu. %s\n", ++words, word ); */ word = strtok( NULL, DELIMITERS ); } free( dup ); } return words; } 

True, why is there a recursion - it is not clear, everything can be done without it:

 #include <string.h> #include <stdio.h> #include <stdlib.h> static const char DELIMITERS[] = " \n\r\t"; static size_t find_set( const char *src, const char *set ) { size_t words = 0; char *dup = strdup( src ); if( dup ) { /* set_length введена для наглядности, см. ниже */ size_t set_length = strlen( set ); char *word = strtok( dup, DELIMITERS ); while( word ) { if( strspn( set, word ) >= set_length ) { /* * эту проверку можно упростить до: * if( !set[strspn( set, word )] ) { */ printf( "%zu. %s\n", ++words, word ); } word = strtok( NULL, DELIMITERS ); } free( dup ); } return words; } int main( void ) { const char src[] = "prostoj primer stroki dlja proverki raboty algoritma, opros."; find_set( src, "rpo" ); return 0; } 
  • TS did not clearly define the notion of "combination" in the formulation of the problem. If ANY combination of the specified characters is meant, then strspn should be used, and if the order of characters is exactly the same as specified, then strstr should be used. - Sergey
  • @Sergey, besides the question there are more comments to it, that says it all. - PinkTux
  • @Sergey strspn(, "oo") that will produce a cooler for the string? - 0andriy
  • Look at the man: "This will be the only way to accept.". Therefore: 0. But the function call strcspn will give exactly what you need: 1. - Sergey