0

I'm trying to make a code that takes tow words from user and store them as arrays and check if the first word's all characters are already exists in the second word , if Yes then just output Yes , if NO then output a NO . Example :

input :

ENTER THE FIRST WORD :

fos

ENTER THE SECOND WORD :

stackoverflow

output :

YES

my code is not even close to what i actually want , but i will put it down maybe it will explain what I`m trying to do .

#include <iostream>
#include <string>
using namespace std;
int main() {
    
    char N1[7];
    char N2[10];
    cout <<  ("ENTER THE FIRST WORD : ") ;
    cin >> N1;
    cout <<("ENTER THE SECOND WORD : ");
    cin >> N2;
    for (int i = 0; i <4; i++)
    {
        for (int j = 0; j < 7; j++)
        {
            if (N1[i] = N2[j]) 
            {
                cout << ("YES") << endl;
            }
            else
            {
                cout << ("NO") << endl;
            }

        }

    }
    return 0;
}

Thanks in advance .

3
  • Tip: unordered_map<string,int>. Also, avoid using namespace std;.
    – Dai
    Commented Sep 9, 2020 at 10:08
  • N1[i] = N2[j] is an assignment, not a comparison. Also, C++ has std::string, no need for awkward char arrays. Also, std::string has std::string::find, which makes your task easier. Also you should really separate processes you can name into a function, in this case something like bool contains_letters(const string& a, const string& b);.
    – Aziuth
    Commented Sep 9, 2020 at 10:18
  • The key concept you are missing is that you can't decide immediately if your condition is true. You can only know at the end of your loops whether every item from the first string is in the second string. Whenever you have a problem that says 'all' you can use a boolean variable. Set the variable to true and then write a loop that tests each item. If any of them fail then set the boolean variable to false. To put it another way, assume you condition is true and first and then look for a single counter example.
    – john
    Commented Sep 9, 2020 at 10:21

3 Answers 3

2

I tried it with simple search and 1 boolean variable to track if it is found or not.

#include <iostream>
#include <string>
using namespace std;



int main()
{
string s1  = "fosq";
string s2 = "stackoverflow"; 
  bool isfound;
  for(int i=0;i<s1.length();i++)
  {
    isfound=false;
    for(int j=0;j<s2.length();j++)
    {
      if(s1[i]==s2[j])
      {
        isfound=true;
        break;
      }
    } 
      if(!isfound)
      {
        cout<<"Not found";
        break;
      }
  }
  if(isfound)
  {
    cout<<"Yes\n";
  }
}

Here what I am doing is that I am looping over both strings, comparing each element. If we can not find any element, I just break the search there, if we find all the elements, we simply just output Yes.

1
  • I have set values of s1 and s2 manually, you can ask for input via cin
    – Ahmad Anis
    Commented Sep 9, 2020 at 10:13
1

I'm not c++ expert but look like you want to check if all char are in the second string - so you better add booleans to your code as:

for (int i = 0; i <4; i++) {
    bool charFound = false;
    for (int j = 0; j < 7; j++) {
        charFound = charFound || (N1[i] == N2[j]); // use OR operator so if found one match it is will stay true
    }
    if (!charFound) {
        cout << ("NO") << endl;
        return; // one char is missing - print no and exit
    }
}
cout << ("YES") << endl; // if got here you found all char - YES

Also notice the == instead of the = in your code (N1[i] == N2[j]) as in Aziuth's comments

0
1

I think the most efficient way is to sort the two arrays before computing the match:

#include <iostream>
#include <algorithm>
#include <array>

template <typename T1, typename T2>
bool match(T1& arr1, T2& arr2) {
    for (char c2: arr2) {
        for (char c1: arr1) {
            if (!c1 || !c2) continue;
            if (c1 == c2) break;
            if (c1 > c2) return false;
        }
    }
    return true;
}
int main() {
    std::array<char, 14> arr1 {"stackoverflow"};
    std::array<char, 4> arr2 {"fos"};

    std::sort(arr1.begin(), arr1.end());
    std::sort(arr2.begin(), arr2.end());
    
    if (match(arr1, arr2)) {
        std::cout << "YES" << std::endl;
    } else {
        std::cout << "NO" << std::endl;
    }
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.