creating a function which takes in two arguments(words) and checks if one is present in the other

Howdy, I am trying to create a function in which i compares two words and sees if one is present in the other and output the remaining word at that occurrence, any help will be greatly appreciated. Here is what i have so far.



#include "std_lib_facilities.h"
#include <stdio.h>
#include <string.h>



char* findx(const char* s, const char* x){


string word_s=x;
string word_x=x;

cout<<word_s<<'\n';
cout<<word_x<<'\n';

}

//------------------------------------------------------------------------------
istream& read_word(istream& is, char* buffer, int max)
// read at most max-1 characters from is into buffer
{
is.width(max); // read at most max-1 characters in the next >>
is >> buffer; // read whitespace terminated word,
// add zero after the last character read into p
return is;
}
//------------------------------------------------------------------------------

istream& read_word_2(istream& is, char* buffer, int max)
// read at most max-1 characters from is into buffer
{
is.width(max); // read at most max-1 characters in the next >>
is >> buffer; // read whitespace terminated word,
// add zero after the last character read into p
return is;
}


//------------------------------------------------------------------------------
int main()
{
const int max = 128;
char s[max];
char x[max];

cout<<"Please type in two word and ill see if one of your words is contained in\n";
cout<<"in the other and output the rest of the word in which it occurred\n";
while (read_word(cin,s,max)&&read_word_2(cin,x,max)){

findx(s,x);
}
}
1
2
3
4
5
6
7
8
9
10
bool findx(std::string first, std::string second){
std::vector<char> similarity; 
for(int i = 0; i < first.length(); i++){
for(int j = 0; j < second.length(); j++){
if(first[i] == second[j]) similarity.push_back(first[i]);
}
}
if(similarity.size() == first.length() || similarity.size() == second.length()) return true;
else return false
}

find a better way to compare the strings with the chars in the array. i did not just want to hand you the answer.
I failed to mention that i cant use the standard library, the goal of this code is to correctly implement arrays and pointers. Well here is my updated code. It just outputs the second word entered and not the remainder of the other word.

Also i understand i don't want the answer handed to me, all i want is for someone to clarify on what Im having issues with.
/* ========================================================================== */

#include "std_lib_facilities.h"
#include <stdio.h>
#include <string.h>



char* findx(const char* s, const char* x){


char copy_w[128];
char copy_x[128];

string word_s=s;// sets the first word entered to word_s
string word_x=x;// sets the second word entered to word_x

for(int i=0;i<word_s.length();++i) { //copies char s into copy_s[] array

copy_s[i]=*s;
++s;
//cout<<copy_s[i];
}
for(int i=0;i<word_x.length();++i){ //copies char s into copy_x[] array

copy_x[i]=*x;
++x;

//cout<<copy_x[i];}
}

for(int i = 0; i<word_x.length();++i){

if(copy_s[i]!=copy_x[i]){// outputs x(question asks print it at the first accurance)


} //Part im stuck on
else

cout<<copy_s[i];

}

}
//------------------------------------------------------------------------------
istream& read_word(istream& is, char* buffer, int max)
// read at most max-1 characters from is into buffer
{
is.width(max); // read at most max-1 characters in the next >>
is >> buffer; // read whitespace terminated word,
// add zero after the last character read into p
return is;
}
//------------------------------------------------------------------------------

istream& read_word_2(istream& is, char* buffer, int max)
// read at most max-1 characters from is into buffer
{
is.width(max); // read at most max-1 characters in the next >>
is >> buffer; // read whitespace terminated word,
// add zero after the last character read into p
return is;
}


//------------------------------------------------------------------------------
int main()
{
const int max = 128;
char s[max];
char x[max];

cout<<"Please type in two word and ill see if one of your words is contained in\n";
cout<<"in the other and output the rest of the word in which it occurred\n";
while (read_word(cin,s,max)&&read_word_2(cin,x,max)){// read in both words


findx(s,x); //function
}
}
Last edited on
your cout will not work without the standard library. also strings are in the standard library so you could not use that either.
I meant to put standard library functions, which of course would make this code that much simpler, sorry for the confusion.
cout is a function. and i only used std::string which you use and a vector, which is a dynamic array. you can switch it out for an array if you want.
Topic archived. No new replies allowed.