0

I need help in figuring out the logic or code to when I want my string not to fall in the middle of another string. For example my given word is "Birthday!" and the other string to look for it is "Happy Birthday Scott". It's going to return a false value because it's missing an exclamation point. Here is the code that I've worked

int Words::matchWords(const char* string, const char* sentence, int wordNum){
int wordCount = words(sentence); // the words function counts the number of words in the sentence
int strLength = strlen(str);
int sentLength = strlen(sentence);
int i = 0;

char strTemp[100];
char sentenceTemp[100];

strcpy(strTemp, str);
strcpy(sentenceTemp, sentence);

    if (wordNum > wordCount) {
        return false;
    }

    char* temp;
    for (i = 0; i < strLength; i++) {
        strTemp[i] = tolower(str[i]);
    }
    for (i = 0; i < sentLength; i++) {
        sentenceTemp[i] = tolower(str[i]);
    }

    temp = strstr(sentenceTemp, strTemp);

    if (temp != NULL) {
        return true;

        if (strTemp[i] != sentenceTemp[i]) { 
            return false;
        }
        else
            return true;

    }
    else
        return false;
}
Yanru Shin
  • 19
  • 6
  • See [https://stackoverflow.com/questions/12784766/check-substring-exists-in-a-string-in-c](https://stackoverflow.com/questions/12784766/check-substring-exists-in-a-string-in-c) – Kagiso Marvin Molekwa Nov 10 '21 at 08:23
  • 2
    First of all, stop using pointers and C-style null-terminated strings. In C++ you should use `std::string`. – Some programmer dude Nov 10 '21 at 08:23
  • 1
    Secondly, please [edit] your question to include the *full* and *complete* assignment, including any and all requirements and limitations. – Some programmer dude Nov 10 '21 at 08:24
  • I am still a beginner, I am used to c programming – Yanru Shin Nov 10 '21 at 08:24
  • 2
    @YanruShin Then you are missing `str.find(substr)!=str.npos;` solution for your problem. Why even learn C++ if you don't want to use its features? – Quimby Nov 10 '21 at 08:28
  • 1
    While learning multiple programming languages is a good thing (but then also learn other languages), the problem is that different languages requires different mind-sets, and you first of all need to disconnect most of what you know about previous languages to learn new ones. C and C++ might look similar, but they are very different. – Some programmer dude Nov 10 '21 at 08:30
  • 1
    Whatever way you write it, turn compiler warnings high, most compilers will report issues with your code. – Öö Tiib Nov 10 '21 at 08:31
  • One reason to switch from C to C++ could be to get rid of all these pointer troubles (without losing the performance which is associated with the use of pointers). Meanwhile C++ has achieved much on this way. You just have to use it... ;-) – Scheff's Cat Nov 10 '21 at 08:35

1 Answers1

2

Here is a super simple program for you to look at.

All you have to do for this problem is create your strings using std::string, determine if they are inside the big string using find(), and lastly check if it was found using string::npos.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string bday = "Birthday!";

    string str1 = "Happy Birthday Scott";
    int found1 = str1.find(bday);
    
    string str2 = "Scott, Happy Birthday!";
    int found2 = str2.find(bday);

    if (found1 == string::npos) //if Birthday! is NOT found!
    {
        cout << "str1: " << "FALSE!" << endl;
    }
    
    if (found2 != string::npos) //if Birthday! IS found!
    {
        cout << "str2: " << "TRUE!" << endl;
    }
}

Note that for string::npos, you use == for something NOT being found and != for something that IS found.

MFerguson
  • 1,739
  • 9
  • 17
  • 30