Algorithm: (break the line into words)

  1. Remember the position of the character.

  2. You go on each character of the line, until you find a space, a period, a comma or any other character separator - this will be the end of the word.

  3. You take a substring from the first position (p.1) to the received one - this will be the word itself.

  4. Go further until the delimiter characters run out. How to end - this is the beginning of the next word. Next - item 1 and so on until the end of the line.

#include <stdio.h> #include <string.h> #include <iostream> using namespace std; main() { char *tempSlova = new char[20]; char *myString = new char[100]; cout << "Vvedite stroky "; gets(myString); cout << "Slova v stroke:" << endl; for(int i = 0, k =0 ; i <= strlen(myString); i++, k ++) { if(myString[i] == ' '|| myString[i] == '\0') { tempSlova[k] = '\0'; if(tempSlova[0]=='\0') tempSlova[0]=0; printf("%s ", tempSlova); k=-1; } else tempSlova[k] = myString[i]; } return 0; } 

Closed due to the fact that off-topic participants Dmitriy Simushev , Kromster , user194374, aleksandr barakin , Denis Bubnov 1 Dec '16 at 8:20 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Dmitriy Simushev, Kromster, Spirit of the community, aleksandr barakin, Denis Bubnov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Well done, this is a step forward. Now make at least one attempt to express this code. - Igor
  • @Igor I can not))) the brain is boiling))) - Dasha Novikova
  • 3
    I'll start you: int main() { ... - Igor
  • @Igor Thank you very much)) you are very kind) - Dasha Novikova
  • I didn’t practice practicing with ++ at university for a long time, but I think the strtok function should quite help you in your algorithm. - binliz

1 answer 1

It is necessary to implement the algorithm as consistently as you have described.

It is completely unclear from your code in what language you write the program: either in C, or in C ++. On the one hand, the C ++ standard output stream is used, on the other hand, the C function.

To begin with, the main function without parameters must be declared in C ++ as

 int main() 

There is no point in dynamically allocating arrays. They could be declared local variables.

The gets function is unsafe, and is no longer even supported by the C standard. Therefore, it should not be used.

If you just need to output the words from the sentence to the console, there is no need to declare an auxiliary array.

To recognize punctuation and spaces, you can use the standard C functions declared in the <cctype> header. The program might look like this.

 #include <iostream> #include <cctype> int main() { const size_t N = 100; char s[N]; std::cout << "Enter a sentence: "; if ( std::cin.getline( s, sizeof( s ) ) ) { for ( const char *p = s; *p; ) { while ( std::ispunct( ( unsigned char )*p ) || std::isspace( ( unsigned char )*p ) ) ++p; const char *first = p; while ( *p && !std::ispunct( ( unsigned char )*p ) && !std::isspace( ( unsigned char )*p ) ) ++p; if ( first != p ) std::cout.write( first, p - first ) << std::endl; } } return 0; } 

The dialogue with the program may look as follows:

 Enter a sentence: first, second, third, fourth, fifth! first second third fourth fifth