I thought of that before, I thought to do so, find the first letter and then rewrite it in the reverse order, and then find the first one again, but it does not work (

#include <cstdlib> #include <iostream> #include <conio.h> #include <stdio.h> #include <string.h> using namespace std; const int S_BUF = 255; int main(){ char str[S_BUF]; cout<<"input text: "; cin.getline(str,255); unsigned int len = strlen(str); unsigned int i; int x = 0; if(str[0]=='c')x++; for(i = 0 ; i < len; i++ )if(str[i]==' '&&str[i+1]=='c')x++; strrev(str); unsigned int j; int y = 0; if(str[0]=='a')y++; for(j = 0 ; j < len; i++ )if(str[j]==' '&&str[j+1]=='a')y++; cout<<"count words on (a) "<<y<<endl; cout<<"count words on (c) "<<x<<endl; system("pause"); return 0; } 
  • 2
    And why not just break the line into words, putting them in a separate array of lines, and then not compare the first and last characters of each word with those required by the task? - MDJHD
  • Can I have a code sample? - zok1995
  • Why not just use regular expressions and replace the whole algorithm with a couple of lines? - Sergue Isupov

1 answer 1

  1. Until the end of the input, look for the beginning of the word.
  2. If the first letter is not "c" go to point 1. Otherwise, we got the index of the beginning of the word.
  3. Until the end of the input, look for the end of the word.
  4. If the end of the word is not "a" go to paragraph 1. Otherwise, we received the index of the end of the word.
  5. Do substr from the beginning to the end of the word on the obtained indices.
  6. Type (write to the array) the word. go to point 1.

Instead of the 5th item, you can accumulate characters into the buffer in p.3.

Threat not to count on the sample code for the training task.

  • Thank you, I will dare) - zok1995