Поиск подстроки в тексте - C (СИ)
Формулировка задачи:
Помогите пожалуйста отладить программу. Задание: поиск подстроки в строке, определить содержит ли строка заданный образец и указать индекс в строке, если совпадение найдено.
#include <stdlib.h>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
char s[20];
char p[20];
int i,j;
int m,n,Result;
m=strlen(s);
n=strlen(p);
printf("Vvedite strochku\n");
scanf ("%s",&s);
printf("Vvedite podstrochku\n");
scanf ("%s",&p);
if(m<n)
{ printf("podstrochka prevyshaet strochku\n ");
}
for (i=0;i<=m-n+1;i++)
for (j=0;j<n;j++)
if (s[i+j]!=p[j])
{
Result=1;
}
if (Result==0);
{printf ("%s",s[i]);
}
if (p[j]<s[i+j-1])
{
Result=0;
printf("podstrochka prevyshaet strochku\n ");
}
if(j=strlen(p))
{
Result=i;
printf ("%s",s[i]);
}
system ("PAUSE");
return 0;
}Решение задачи: «Поиск подстроки в тексте»
textual
Листинг программы
#include <stdio.h>
#include <string.h>
int main(void)
{
const char line[] = "abcd pattern pattern defg";
const char patt[] = "pattern";
char *p;
if ((p = strstr(line, patt)) != NULL)
printf("%s: index = %d\n", patt, p - line);
else
printf("none\n");
return 0;
}