Detecting a word

closed account (G26pX9L8)
Hello,

ive a little question. how do I know if a given word in a sentence, so I then can perform an action?
Example:
this is an example
check the word:
example
now if example is found i what to run some code. Does anybody know how to solve this?
Last edited on
This is as far as I got, sorry I know it doesn't work. At least the concept is right! :D If you or anyone would make it work that would be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <windows.h>
#include <winuser.h>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>

using namespace std;

string input;
char convert[50];
char word1[50];
char word2[50];
char word3[50];
char space[]=" ";
bool go = false;

int main()
{
	input = "he man example";
	strcpy_s(convert, input.c_str());
		for (int i=0; i<50; i++)
		{
			while (convert[i] != space[0])
			{
				go=true;
				word1[i]=convert[i];
				while (go)
				{
					i++;
					go=false;
				}
			}
			while (convert[i] != space[0])
			{
				go=true;
				word2[i]=convert[i];
				while (go)
				{
					i++;
					go=false;
				}
			}
			while (convert[i] != space[0])
			{
				go=true;
				word3[i]=convert[i];
				while (go)
				{
					i++;
					go=false;
				}
			}
		}
	if (word1 == "example")
		cout << "word1!\n";
	if (word2 == "example")
		cout << "word2!\n";
	if (word3 == "example")
		cout << "word3!\n";
	return 0;
}
Here is how you could do it:

1
2
3
4
5
6
7
8
9
10
	std::string str;
	getline(std::cin, str);

	for(std::string::size_type i = 0; i < str.size(); ++i)
	{
		if(!isalpha(str[i]))
		{
			str[i] = ' ';
		}
	}

First, read a whole line into a std::string and get rid of any character that isn't a letter. This will keep you from reading something like "example." as if it were a word. Do notice that this prevents the program from reading words with the character '-' in them, so adjust as needed.

1
2
3
4
5
6
7
8
9
10
11
12
	std::stringstream buffer;
	buffer << str;
	while(buffer)
	{
		std::string s;
		buffer >> s;
		if(s == "example")
		{
			std::cout << s << std::endl;
			break;
		}
	}

Then, parse the buffer using a std::stringstream and check for the word you're looking for. In this case, we simply output the word and break from the loop, but in a real program this should really be inside a function, something like bool f(std::string line, std::string word) that would return if it found the word.

One last thing: this really has nothing to do with Windows so I guess it doesn't fit the Windows programming forum.
closed account (D80DSL3A)
This solution is based on the use of a standard string function strstr( char* s1, char* s2) which returns a pointer to the location of s2 within s1. It is a console app (not a windows app).
I have initialized a char* to NULL here. If it stays NULL it is because the word was not found within the sentence. Note: As written the sentence length is limited to 49 characters so adjust if needed for a longer one.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>// for use of cin, cout
#include <cstdio>// for use of gets(), scanf()
#include <cstring>// for use of string functions strcpy(), strcmp(), strlen(), strcat(), strstr(), etc.

using namespace std;


int main()
{
	char bigString[50];
	char word[50];	
	char* pLocation = NULL;
	char a = 'n';

	do  // allows program to repeat without restart
	{		
		cout << "enter big string:";
		gets_s(bigString);
		cout << "enter a word:";
		gets_s(word);
	
		pLocation = strstr(bigString, word);// this string function returns pointer to 2nd string within 1st
		if( pLocation == NULL )
			cout << word << " is not in the sentence: " << bigString << endl;
		else
			cout << word << " is in the sentence: " << bigString << endl;
	
		cout << "repeat (y/n)?" << endl;
		scanf_s("%c", &a);
		_flushall();
               pLocation = NULL;// reset for next loop
	}while( a=='y');// if anything other than 'y' is entered program will terminate

	return 0;
}


Are you trying to create the functionality of the strstr() function yourself? If so then this "solution" misses the point of your exercise.
Last edited on
closed account (G26pX9L8)
OK, tnx all. I'm going to work tonight and then I will let you know more. And it is true that this is not the proper forum.
Topic archived. No new replies allowed.