-
...
-
int i=0,j=0,ag=0;
-
-
while(sentence[i]!='/0')
-
{
-
while(word[j]!='/0')
-
{
-
if((int)sent[i]==(int)word[j])
-
{
-
i++;
-
j++;
-
flag++;
-
}
-
else
-
{
-
i++;
-
j=0;
-
}
-
}
-
}
-
-
if(flag==strlen(word))
-
cout<<"word found";
-
else
-
cout<<"nt found";
-
A couple of problems in this code
- This code assumes that if you are looking for a word of length N then it the word does not occur at sentance[i] it wont occur at sentence[i+1], sentence[i+2] ... sentence[i+N-1]. i.e. It only checks every Nth character of sentence for the search word. This is because you increment the variable i inside the second loop.
- If it finds the word then it still carrys on searching the rest of the sentence, most search algorithms return immediately to save processor time. This is because you don't break the outer loop once you have found the word. In fact if the characters of the word exist in the sentence outside the word then flag will get erroneously update resulting in a match fail because it will be > strlen(word)
- You perform an unneccessary strlen at the end, you already know if you found the word, no need for this it is a waste of time, you need to set a flag instead of incrementing a flag.
- Finally you risk a memory access error, you loop i right up to the end of the sentence but then internall you loop i right up to the end of the word so the maximum size you check is sent[iMax+jMax]. Very possibly outside the range of the array the sent points to. You do not need to check the last characters of sent if there are not enough characters left to fit word into.
- This is not an error but on a point of style you have used while loops, however you are looping up the charaters of an array, a for loop would be more common and will make the code look neater.
Problem 1 this is caused by the code fragment
-
if((int)sent[i]==(int)word[j])
-
{
-
i++;
-
j++;
-
flag++;
-
}
-
else
-
{
-
i++;
-
j=0;
-
}
-
By incrementing i you prevent a proper search at all the characters of sentence, we need to remove the increment of i. However we still need to loop down sentence by the size of word. This is in fact what j does. Also casts are completely unrequired (assuming sentence and word are both arrays of char or char *). So we re-write this as
-
if( sentence[i+j]==word[j])
-
{
-
j++;
-
flag++;
-
}
-
else
-
{
-
j=0;
-
}
-
Having done this the j=0; line causes problems, because it will causes the code to start searching from the beiginning of the word again as soon as it finds a non-matching letter. We need to remove it resulting in
-
if( sentence[i+j]==word[j])
-
{
-
j++;
-
flag++;
-
}
-
Problem 2 + 3
Caused by the code fragment
-
while(word[j]!='/0')
-
{
-
if( sentence[i+j]==word[j])
-
{
-
j++;
-
flag++;
-
}
-
}
-
flag++; is not useful, lets get rid of it we already know it can count erroneous characters giving an incorrect result. Also now we only increment j if we find a letter match. We are seraching the sentence at i for word, as soon as we find a letter mismatch then we stop the loop and go on to sentence[i+1]. If the letters match we need to increment j to test the next letter in the word. If we get to the end of the loop then we have found a match for the word. Also we need to start the search from the beginning of the word each time so j should be initialised to 0 before the loop starts. This code becomes
-
j = 0;
-
while(word[j]!='/0')
-
{
-
if( sentence[i+j] != word[j])
-
{
-
break;
-
}
-
-
j++;
-
}
-
-
if ( word[j] == '\0')
-
{
-
// word found set flag and break or return
-
}
-
Also the strlen test from the end of the code can be removed
Problems 4 and 5
Because you have used while loops the loop control variables are checked and later all over the place substituting with for loops keeps it all together and makes the code neater. Additionally there was nothing to stop the code looking off the end of the array sentence, because the inner loop now always goes to a maximum of strlen(word) and because you don't need to check sentence once you have got to the point where there are not enough characters left to contain word you can stop, this is at the limit strlen(sentence)-strlen(word)
Starting from
-
int i=0,j=0,ag=0;
-
-
while(sentence[i]!='/0')
-
{
-
j = 0;
-
while(word[j]!='/0')
-
{
-
if( sentence[i+j] != word[j])
-
{
-
break;
-
}
-
-
j++;
-
}
-
-
if ( word[j] == '\0')
-
{
-
// word found set flag and break or return
-
}
-
-
i++;
-
}
-
this gives
-
int i;
-
int j;
-
-
for(i=0; i < strlen(sentence)-strlen(word); i++)
-
{
-
for( j=0; word[j] != '/0'; j++)
-
{
-
if( sentence[i+j] != word[j])
-
{
-
break;
-
}
-
}
-
-
if ( word[j] == '\0')
-
{
-
// word found set flag and break or return found
-
}
-
}
-
-
// Access flag or return not found here
-
Which is the solution, is less complex and is a couple of lines shorter