0

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 by Smartflight: n/a

4
Contributors
7
Replies
11
Views
5 Years
Discussion Span
Last Post by Smartflight
Featured Replies
  • Sorry. I must have missed part of that. I'll look at this: [url]http://www.c4learn.com/2010/08/c-program-to-find-substring-of-string.html[/url] ...and see if it can/should be modified or updated. Read More

  • You don't have to call [ICODE]strlen[/ICODE] or any other function to compute your word's length. Notice that, inside your loop, [ICODE]c[/ICODE] represents the number of correct consecutive letters. Just add a bool variable outside your loop and modify it appropriately inside the loop: [CODE]bool found = false; while(true) { if … Read More

  • 1
    WaltP 2,905   5 Years Ago

    [QUOTE=Smartflight;]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. [CODE]#include <iostream.h> #include <stdio.h> #include <string.h> void main () { char para[200], word[20]; … Read More

0

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 by thines01: Added Code

0

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?

0

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 by thines01: typo

1

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

    //...
}
1

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.

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 by Smartflight: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.