#include <stdio.h> #include <conio.h> #include <string.h> int main(int argc, char* argv[]) { char str[100]; char substr[50]; int i; printf("Vvedi stroku:"); gets(str); printf("Vvedi podstroku:"); gets(substr); char* p; p=strstr(str, substr); printf("Pozicii vhozhdeniya: %s\n", p); i=0; while (p=strstr(p,substr)) { i++; p++; } printf("Vsego Vhozhdenii: %d\n", i); _getch(); return 0; } 

It is necessary that the program prints the positions of the occurrence of the second line in the first and their total number. My program considers the occurrence of only 2 identical lines and does not determine the position of the occurrence. Tell me, please, how can this be fixed?

  • one
    Apparently read the man strstr and if something remains unclear, go back to the chapter on pointers and address arithmetic in the C (or C ++) book. - avp
  • <pre> int main (int argc, char * argv []) {char str [100], substr [50]; printf ("Vvedi stroku:"); gets (str); printf ("Vvedi podstroku:"); gets (substr); char * p; // c is the count of occurrences. // i is the index of the character from which we are looking for a substring. for (int c = 1, i = 0; i <100 && (p = strstr (& str [i], substr)); i ++) printf ("Poziciya vhozhdeniya #% d:% d \ n", c ++, i = p - str); _getch (); return 0;} <code> I tried this option. I enter the 1st row, 2nd row. The program does not display the result. What could be the reason? - siner

0