function search by string

0

I'm trying to implement a method that tells me how many zones are in the vector I'm using the method strcmp but I get an error I'm using the library #include <string.h> is the first time I use it and not I know what to do here I leave my code

void ContarPorZona(string zona){
    int cont=0,cont2=0;
     for(int k=0;k<n;k++){
    if(strcmp (zona,"residencial") == 0){
        cont++;
    }
    if(strcmp (zona,"parque") == 0){
        cont2++;
    }
    }
    cout<<"la cantidad de clientes residenciales son: "<<cont<<endl;
    cout<<"la cantidad de clientes del parque son: "<<cont2<<endl;
}
    
asked by Rigoberto Oviedo Bolaños 19.11.2017 в 20:27
source

1 answer

1

It would be good if you put your code inside the { } tags and offer a little more detail about your problem, as well as a little more about your code to better see your situation.

The strcmp function is used to compare a string with another string. This returns the value 0 if they are equal, but in your case you want to count the number of times a string is repeated.

Example:

if(!strcmp(str1, str2))
{
 cout<<"Son iguales";
 vecesRepetidas++; // Variable contadora tipo INT
}

Another way is with:

if (strcmp(str1,str2) == 0)

Another way is with string::compare , returns 0 if they are the same:

std::string str1 ("manzana");
std::string str2 ("coco");

  if (str1.compare(str2) == 0)
    vecesRepetidas++; // Variable contadora tipo INT

Including a counter variable you can know how many times a string has been repeated.

I hope it serves you.

    
answered by 19.11.2017 / 20:56
source