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?

Edited 4 Years Ago by Smartflight: n/a

What about the .find() method on the string?
Example:

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
   string strBase = "this is neat";
   string strOne = " be ";
   string strTwo = " is ";

   cout << ((-1 == strBase.find(strOne))? "Not Found" : "Found") << endl;
   cout << ((-1 == strBase.find(strTwo))? "Not Found" : "Found") << endl;

   return 0;
}

Edited 4 Years Ago by thines01: Added Code

Like I said, I don't want to use any library function. If one is required, I must build a replica function myself that would do what the library function would.

The question is, is there another approach to it, that does not involve a library function such as strlen() or for that matter, any other function?

Please try this:
Re-write that function on that page so the parameters are const and it will work for your purposes.
You will need to call the .c_str() method on both the original string and the searched-for string.

Edited 4 Years Ago by thines01: typo

You don't have to call strlen or any other function to compute your word's length.
Notice that, inside your loop, c represents the number of correct consecutive letters.
Just add a bool variable outside your loop and modify it appropriately inside the loop:

bool found = false;

while(true)
{
    if (word[c] == '\0' && ( /* rather verbose condition, involving spaces */ )) { found = true; break; }

    if (para[i] == '\0') break; // found == false

    //...
}

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?

1) Read this about void main() .
2) Read this about gets() .
3) Just use a loop to count the characters in word up to the \0.

Thank you everyone for the reply. The Boolean variable seems like a good solution, and so does the function on c4learn.com

Also, thanks for this Walt:

1) Read this about void main() .
2) Read this about gets() .

Edited 4 Years Ago by Smartflight: n/a

This question has already been answered. Start a new discussion instead.