-1
    char words[5] = { 't', 'd' , 'o', 'g', 'i', };
    cout << "enter letter ";


    char dogsearch[3] = { 'd', 'o', 'g' };

    if (words == dogsearch)
        cout << "found words";
    else
        cout << "Not found word";

i have two arrays and comparing them with each other to find the similiarity, and differences within each another, if there is similiar i want to output how it is, and vice versa

kas tozu
  • 11
  • 5
  • 1
    `if (words == dogsearch)` actually compares two pointers which will never be the same. – πάντα ῥεῖ Mar 26 '16 at 22:48
  • 1
    `string sentence(words);` This exhibits undefined behavior by way of buffer overrun, since `words` is not NUL-terminated. Make it `string sentence(words, 5);`, or use `std::find` algorithm on the original array. – Igor Tandetnik Mar 26 '16 at 22:49
  • 1
    Even if, by some miracle, `words == dogsearch` compared the contents of the two arrays - what's the point? You already know the contents are different - you initialized them yourself, to different values. It's not at all clear what you are trying to achieve here. What do you want to find where? – Igor Tandetnik Mar 26 '16 at 22:51
  • @IgorTandetnik i am trying to make a software that, that finds "Dog" in the first array and states, what position it is in. Any advice – kas tozu Mar 26 '16 at 22:56
  • `cout << "found 'dog' at position 2";` satisfies stated requirements. – Igor Tandetnik Mar 26 '16 at 22:58
  • @IgorTandetnik no i want that to be the outcome but, how would i go about coding it. – kas tozu Mar 26 '16 at 23:01
  • 1
    Well, actually formulating a problem you are trying to solve might be a good start. – Igor Tandetnik Mar 26 '16 at 23:22
  • I agree with Igor. Its still unclear what you want to achieve, since you already have answers to your problem statement which clearly solve it. Please try to state clearly what you want to do and why a simple string::find doesn't solve your problem. Are the arrays even important for you? Or are you simply trying to find a substring in another string? – pokey909 Mar 26 '16 at 23:36
  • I'd suggest some edificating readings http://stackoverflow.com/a/388282/4743711 – Loreto Mar 27 '16 at 00:09
  • https://en.wikipedia.org/wiki/Longest_common_substring_problem – Edward Strange Mar 27 '16 at 18:01

1 Answers1

0

You can use string::find to look for substring matches

char words[5] = { 't', 'd' , 'o', 'g', 'i', };
char dogsearch[3] = { 'd', 'o', 'g' };

string words_str(words, sizeof(words));
string needle(dogsearch, sizeof(dogsearch));

size_t pos = words_str.find(needle);
if (pos != std::string::npos)
   cout << "found words at position " << pos << '\n';
else
   cout << "Not found word;
pokey909
  • 1,797
  • 1
  • 16
  • 22
  • Thanks for that, what does != std::string::npos)? mean could you explain, further is there a way to see what position the first character of the word is in? – kas tozu Mar 26 '16 at 23:09
  • @kastozu `words_str.find(needle)` returns the position of the first character of the first match. – DimChtz Mar 26 '16 at 23:10
  • see edit, for an updated version. To whoever it was with the downvote: It would be more helpful if you actually give a suggestion how to improve the post. – pokey909 Mar 26 '16 at 23:13
  • It wasn't me downvoting, but I think creating strings from the start would be better – Incomputable Mar 26 '16 at 23:15
  • npos is a static member constant value with the greatest possible value for an element of type size_t. As a return value, it is usually used to indicate no matches. – El pupi Mar 26 '16 at 23:15
  • @olzhas, absolutely agree. I was just sticking to the OP's version. He might have his reasons for the arrays. – pokey909 Mar 26 '16 at 23:16
  • instead of using (pos != std::string::npos), is there an alternative as i am not familiar with it. I apperciate the help pokey – kas tozu Mar 26 '16 at 23:17
  • npos is usually -1, but it is implementation defined, so if you write just -1 your code will be unportable – Incomputable Mar 26 '16 at 23:20
  • @kas you should be familiar with it since its the normal way how string::find works. http://www.cplusplus.com/reference/string/string/find/ – pokey909 Mar 26 '16 at 23:23
  • @pokey909 once again appreciate the help – kas tozu Mar 26 '16 at 23:27