How to intersect two text strings?

0

With the intersection I mean that if I have two sentences I keep the words and not the same characters

//Programa para la interseccion de dos cadenas de texto

#include<iostream>
using namespace std;

int main(){

string x,y,frase;
cout<<"Ingrese los elementos de x: ";
 getline(cin,x);
cout<<"Ingrese los elementos de y: ";
 getline(cin,y); 
cout<<"Elementos de x: "<<endl;
for(int i=0; i<x.length();i++){
    cout<<x[i];
}
cout<<"\n"<<endl;
cout<<"Elementos de y: "<<endl;
for(int j=0; j<y.length();j++){
    cout<<y[j];
}
cout<<"\n"<<endl;
cout<<"Interseccion: "<<endl;
for(int i=0; i<x.length();i++){
    for(int j=0; j<y.length();j++){
        if(x[i]==y[j]){
            cout<<x[i];//Me produce error ya que cuento los caracter
        }
    }
}
return 0;
}
    
asked by Trapss 19.05.2018 в 20:13
source

2 answers

0

Good morning,

I see that your code requires identifying the words of both sentences that are the same. However, what it does is identify which letters in both sentences are coincident.

I solved it this way:

  • We enter the sentence X and Y.
  • Let's count how many words are in sentence X.
  • We create a vector with that dimension.
  • We introduce the words of sentence X in that vector.
  • Let's count how many words are in the Y sentence.
  • We create another vector with that dimension.
  • We introduce the words of the sentence Y into that vector.
  • We compare all the elements of the VectorX with all the VectorY elements to see which words are Coincident.

It can be improved in many ways, for example instead of counting words and then introducing them, you can create a dynamic vector that counts and auto-dimension as you detect new words. You can also make it not discriminate between uppercase and lowercase letters, because it only detects the exact words.

//Programa para la interseccion de dos cadenas de texto

#include<iostream>
using namespace std;

int main(){
    int contadorx, cantidadx, cantidady, contadory;
    string x,y,frase,palabrax, palabray;
    cout<<"Ingrese los elementos de x: ";
    getline(cin,x);
    cout<<"Ingrese los elementos de y: ";
    getline(cin,y); 

    cout << endl;

    /* Verificamos la cantidad de palabras en la primer oracion */
    cantidadx=0;
    contadorx=0;


    while (contadorx<x.length())
    {
        while(x[contadorx]!=' ' && contadorx<x.length()){
            contadorx++;            
        }
        contadorx++;
        cantidadx++;        
    }

    /* creamos el vector para contenerlas */

    string VectorX[cantidadx];

    cantidadx=0;
    contadorx=0;
    palabrax="";

    while (contadorx<x.length())
    {
        while(x[contadorx]!=' ' && contadorx<x.length()){
            palabrax=palabrax+x[contadorx];
            contadorx++;            
        }
        VectorX[cantidadx]=palabrax;
        contadorx++;
        cantidadx++;        
        palabrax="";
    }

    cout << "Elementos de X: " << endl;

    for (int i=0;i<cantidadx;i++){
        cout << VectorX[i] << endl;
    }

    /* Fin de insertar palabras en el vector de X */

    cout << endl;
    cout << "Elementos de Y: " << endl;
    /* Verificamos la cantidad de palabras en la segunda oracion */
    cantidady=0;
    contadory=0;        
    while (contadory<y.length())
    {
        while(y[contadory]!=' ' && contadory<y.length()){
            contadory++;            
        }
        contadory++;
        cantidady++;        
    }

    /* creamos el vector para contenerlas */

    string VectorY[cantidady];

    cantidady=0;
    contadory=0;
    palabray="";

    while (contadory<y.length())
    {
        while(y[contadory]!=' ' && contadory<y.length()){
            palabray=palabray+y[contadory];
            contadory++;            
        }
        VectorY[cantidady]=palabray;
        contadory++;
        cantidady++;        
        palabray="";
    }

    for (int i=0;i<cantidady;i++){
        cout << VectorY[i] << endl;
    }

    cout << endl;
    /* Fin de insertar palabras en el vector de X */

    /*Empezamos a ver que palabras coinciden */
    for (int xx=0;xx<cantidadx;xx++){
        for (int yy=0;yy<cantidady;yy++){
            if (VectorX[xx]==VectorY[yy]){
                cout << "Coinciden: " << VectorX[xx] << endl;
            }
        }
    }
return 0;
}
    
answered by 22.05.2018 / 14:26
source
0

The response of @HernanL . it may work, but it is not recommended because, among other things, it uses VLA , something that is not supported by the standard.

To read the strings I would bet, if you have room to maneuver, to make use of the STL, which is for something:

std::vector<std::string> LeerLinea()
{
  std::string linea;
  std::getline(std::cin,linea);

  std::vector<std::string> toReturn;
  std::istringstream iss(linea);
  std::copy(std::istream_iterator<std::string>(iss),
            std::istream_iterator<std::string>(),
            std::back_inserter(toReturn));

  return toReturn;
}

What this function does is basically read a full line of text and store it in linea , then store the string in a special stream ( istringstream ) and not , it can not be done directly.

The grace of storing the line read in this stream is that it will allow us to use the function copy to iterate over each word. back_inserter is a utility function that, whenever invoked, calls the push_back method of toReturn . In short ... each word is taken and added to the vector toReturn .

With this we can easily read the two lines of entry:

std::cout<<"Ingrese los elementos de x: ";
std::vector<std::string> x = LeerLinea();
std::cout<<"Ingrese los elementos de y: ";
std::vector<std::string> y = LeerLinea();

And we only need to calculate the intersection. Coincidentally, in the library algorithm there is a function that calculates the intersection between two collections of elements ... look at your things, then the easiest way to end the program could be to use this function:

std::vector<std::string> interseccion;

std::set_intersection(std::begin(x),std::end(x),
                      std::begin(y),std::end(y),
                      std::back_inserter(interseccion));

What set_intersection does is fill in the vector interseccion ... it does not have much mystery. We can verify that the result is correct by simply iterating over the vector iteracion :

std::cout << "Interseccion:\n";
for( std::string palabra : interseccion )
  std::cout << "  " << palabra << '\n';
    
answered by 22.05.2018 в 15:55