The aim is simple: Input a string from the user, input a word that is to be searched in the string, and return "Found" or "Not found"
I have worked down the following code, and it works.
#include <iostream.h>
#include <stdio.h>
#include <string.h>
void main ()
{
char para[200], word[20];
int i=0, c=0;
cout<<"\nEnter a paragraph: ";
gets (para);
cout<<"\nEnter the word you want to search: ";
gets (word);
while (para[i]!='\0')
{
if (para[i]==word[c] && word[c]!='\0' && para[i]!=' ')
c++;
else
c=0;
i++;
}
if (c==strlen(word))
cout<<"\nWord found"<<endl;
else
cout<<"\nWord not found"<<endl;
}
I want to do it without having to use the strlen() function but I'm clueless. I don't want to use any library function for the search of the word in the string.
Edit: I could also count the number of letters in the character array 'word' and store it in an integer, then compare c with that integer, instead of using strlen().
Is there any other entirely different approach for achieving this?