Searching CString for certain phrase

I'm trying to write a function, findHaHa(), that searches the cstring the user input and returns whether the phrase "ha ha ha" was found. I've looked up how to do it but only found how to find it using strings but I was told to only use the cstring library. I also found how to find a word, but not a phrase.

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
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstring>
using namespace std;

int wordCount(char str1[]);
int vowelCounter(char str1[]);
int numCount(char str1[]);
int findHaHa(char str1[]);

int main()
{ 
	char str1[500];

	cout << "Enter the string" << endl;

	int size = strlen(str1);

	cin.getline(str1, 500);                         // takes in string


	cout << "Number of words: " << wordCount(str1) << '\n' << "Number of vowels: " << vowelCounter(str1) << '\n' 
		<< "Number of numerals: " << numCount(str1) << endl;

}

int wordCount(char str1[])
{
	int numWords = 0;
	for (int i = 0; str1[i] != 0; i++)        // goes through the string until reaches NULL
	{
		if (str1[i] == ' ')                   // counts a word for each space
		{
			numWords++;
		}
	}

	return numWords + 1;                    // add one because there is a word after the last space
}

int vowelCounter(char str1[])
{
	int numVowels = 0;

	for (int i = 0; str1[i] != 0; i++)
	{
		if (str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i] == 'u' 
			|| str1[i] == 'A' || str1[i] == 'E' || str1[i] == 'I' || str1[i] == 'O' || str1[i] == 'U')
		{
			numVowels++;
		}
	}
	return numVowels;
}

int numCount(char str1[])
{
	int numNum = 0;
	
	for (int i = 0; str1[i] != 0; i++)
	{
		if (str1[i] == '1' || str1[i] == '2' || str1[i] == '3' || str1[i] == '4' || str1[i] == '5'
			|| str1[i] == '6' || str1[i] == '7' || str1[i] == '8' || str1[i] == '9' || str1[i] == '0')
		{
			numNum++;
		}
	}
	return numNum;
}

int findHaHa(char str1[])
{
	
}
Last edited on
I also found how to find a word, but not a phrase.

Finding a phrase would be similar to finding a word, so perhaps you should start by finding a word.

Do you realize that (char str1) means a single character not a C-string?

Are you allowed to use some of the <cctype> functions? They might make finding how many digits much easier.

By the way your word count is not really correct since there is more than one type of "space" character. Again one of the <cctype> functions would help.




Last edited on
The function you're looking for is strstr

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
#include <iostream>
#include <cstring>
using namespace std;

int main() 
{
	const char* string1 = "ha ha ha";
	const char* string2 = "beans ha ha ha kittens";
	const char* string3 = "no words here";
	
	if (strstr( string2, string1))
	{
		cout << string1 << " is in " << string2 << '\n';
	}
	else
	{
		cout << string1 << " is not in " << string2 << '\n';
	}
	
	if (strstr ( string3, string1 ))
	{
		cout << string1 << " is in " << string3 << '\n';
	}
	else
	{
		cout << string1 << " is not in " << string3 << '\n';
	}
	
	return 0;
}
if you want to do this yourself, without using strstr (which is a very efficient routine), your approach seems overly convoluted.

I think I would iterate the target string, searching for the first letter only of the string you seek. If you find that, a memcmp of the target against the source using the strlen of target to verify that you found it is what you want to do. Both memcmp and strlen can be done by hand as simple for loops if you REALLY want to go into wheel recreation exercises.

if you want to kick them up a notch in speed you can cast all the string bytes into integers and compare blocks instead of bytes. This takes a little effort to handle the blocks that are smaller than the size of the int, but its nothing too difficult.

remember that strstr only gets the first one it finds. You can call it again from the resulting pointer + 1, or +strlen of the thing, depending on whether you accept internal matches or not (that is, does aaaaaa seeking aa find 3 or 5 matches??) if you want to find all the matches or count them etc.

Last edited on
now I have this but when I run it, it always says that "ha ha ha" was found, even if it wasn't in the string. Does it have to do with me setting a variable equal to the function call on line 26? How else would I write an if statement using the return of a function in main()?

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstring>
using namespace std;

int wordCount(char str1[]);
int vowelCounter(char str1[]);
int numCount(char str1[]);
int findHaHa(char str1[]);

int main()
{ 
	char str1[500];

	int hahaFound = 0;

	cout << "Enter the string" << endl;

	int size = strlen(str1);

	cin.getline(str1, 500);                         // takes in cstring


	cout << "Number of words: " << wordCount(str1) << '\n' << "Number of vowels: " << vowelCounter(str1) << '\n' 
		<< "Number of numerals: " << numCount(str1) << endl;
	
	hahaFound = findHaHa(str1);                   //hahaFound is now equal to what the function returned

	if (hahaFound = 0)
		cout << "ha ha ha was not found in the string." << endl;
	else
		cout << "ha ha ha was found in the string." << endl;
}

int wordCount(char str1[])
{
	int numWords = 0;
	for (int i = 0; str1[i] != 0; i++)        // goes through the cstring until reaches NULL
	{
		if (str1[i] == ' ')                   // counts a word for each space
		{
			numWords++;
		}
	}

	return numWords + 1;                    // add one because there is a word after the last space
}

int vowelCounter(char str1[])
{
	int numVowels = 0;

	for (int i = 0; str1[i] != 0; i++)                //searches entire cstring for vowels
	{
		if (str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i] == 'u' 
			|| str1[i] == 'A' || str1[i] == 'E' || str1[i] == 'I' || str1[i] == 'O' || str1[i] == 'U')
		{
			numVowels++;
		}
	}
	return numVowels;
}

int numCount(char str1[])
{
	int numNum = 0;
	
	for (int i = 0; str1[i] != 0; i++)                  //searches entire cstring for numbers
	{
		if (str1[i] == '1' || str1[i] == '2' || str1[i] == '3' || str1[i] == '4' || str1[i] == '5'
			|| str1[i] == '6' || str1[i] == '7' || str1[i] == '8' || str1[i] == '9' || str1[i] == '0')
		{
			numNum++;
		}
	}
	return numNum;
}

int findHaHa(char str1[])
{
	const char* str2 = "ha ha ha";

	if (strstr(str1, str2))                 // ha ha ha was found
		return 1;
	else
		return 0;                    // ha ha ha was not found
}
Last edited on
I have a bad headache, so I am going to go full bore simple here.

look.

char str[] = "blahblahhahah"
char * isitthere = 0;
isitthere = strstr(str, "xyz");
if(isitthere)
cout << "yep" << endl;
else
cout << "nope" << endl;

now change it to look for "haha" and it will find it.

the result of strstr is a pointer to the first letter of the target string if it exists, else null.

I have no idea what you are doing wrong, but that is how you use this stuff.
if (hahaFound = 0)
is wrong. Try
if (hahaFound == 0)
Last edited on
Repeater, thank you so much!
Topic archived. No new replies allowed.