Answer the question
In order to leave comments, you need to log in
Why does the search for a word in a string not work?
Good day, I wrote in C ++ an algorithm for finding the first occurrence of a word in a string. The function should return the position number of the first occurrence of the word in the string:
#include <iostream>
using namespace std;
int search(char t[9], char p[3])
{
int n = strlen(t), m = strlen(p), i = 0, j;
while (i <= (n-m)) //<----------------------тут краш
{
j = 0;
while ((t[i+j] = p[j]) && (j < m))
j++;
if (j = m)
return i;
i++;
}
}
int main()
{
cout << search("badbarrar", "bar");
return 0;
}
Answer the question
In order to leave comments, you need to log in
int search(char t[9], char p[3])
Try like this:int search(char t[], char p[])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question