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.
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.
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.
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()?
#include <iostream>
#include <cstring>
usingnamespace 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[])
{
constchar* str2 = "ha ha ha";
if (strstr(str1, str2)) // ha ha ha was found
return 1;
elsereturn 0; // ha ha ha was not found
}