Comparer deux string
Fermé
Onur78
Messages postés
27
Date d'inscription
dimanche 19 septembre 2010
Statut
Membre
Dernière intervention
6 juin 2014
-
5 janv. 2013 à 20:03
mamiemando Messages postés 33591 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 20 mars 2025 - 6 janv. 2013 à 13:34
mamiemando Messages postés 33591 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 20 mars 2025 - 6 janv. 2013 à 13:34
A voir également:
- Comparer deux string c++
- Deux ecran pc - Guide
- Comment faire deux colonnes indépendantes dans word - Guide
- Itinéraire google map entre deux adresses - Guide
- Deux whatsapp sur un téléphone - Guide
- I deux point ✓ - Forum Windows
3 réponses
Salut,
je suis pas un grand expert c++,
mais tu pourrais t'inspirer de ceci:
et un petit lien utile!! ;)
http://www.cplusplus.com/reference/string/string/string/
@++
je suis pas un grand expert c++,
mais tu pourrais t'inspirer de ceci:
string StringToUpper(string mot){ string res; int i; for(i = 0;i<mot.size();i++){ res+=toupper(mot.at(i)); } return res; } bool compareText(string mot,string text){ string MOT= StringToUpper(mot); string TEXT=StringToUpper(text); return MOT.find(TEXT) != string::npos; } int main() { string str = "Hi Onur noob"; if(compareText(str,"Onu noob")) cout << "You are the noob" << endl; else cout << "Erreur" << endl; return 0; }
et un petit lien utile!! ;)
http://www.cplusplus.com/reference/string/string/string/
@++
Onur78
Messages postés
27
Date d'inscription
dimanche 19 septembre 2010
Statut
Membre
Dernière intervention
6 juin 2014
5
Modifié par Onur78 le 6/01/2013 à 10:53
Modifié par Onur78 le 6/01/2013 à 10:53
J'ai finalement trouvé la réponse moi même ^^ en cherchant.. cherchant et cherchant....
find pour dire de trouver, et nocase pour ignorer la casse. J'espère que ça aideras certaines personnes, je n'ai pas trouvé sur CCM
else if(str_find_nocase(pMsg->m_pMessage, "onur noob")) { Server()->Kick(ClientID, "You are the noob"); }
find pour dire de trouver, et nocase pour ignorer la casse. J'espère que ça aideras certaines personnes, je n'ai pas trouvé sur CCM
mamiemando
Messages postés
33591
Date d'inscription
jeudi 12 mai 2005
Statut
Modérateur
Dernière intervention
20 mars 2025
7 834
Modifié par mamiemando le 6/01/2013 à 13:41
Modifié par mamiemando le 6/01/2013 à 13:41
En C++ tu peux par exemple écrire une fonction to_lower qui calcule la chaîne écrite en minuscule. Ensuite il suffit d'utiliser l'opérateur ==.
.. ou encore coder directement la fonction de comparaison :
Sortie :
Bonne chance
#include <string> #include <iostream> #include <algorithm> std::string to_lower(const std::string & sin) { std::string s(sin); std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } int main() { std::string s1 = "TotO"; std::string s2 = "TOTO"; if (to_lower(s1) == to_lower(s2)) { std::cout << "s1 = " << s1 << " <=> s2 = " << s2 << std::endl; } return 0; }
.. ou encore coder directement la fonction de comparaison :
#include <string> #include <iostream> bool stricmp(const std::string & s1, const std::string & s2) { std::string::const_iterator s1_it (s1.begin()), s1_end(s1.end()), s2_it (s2.begin()), s2_end(s2.end()); for(; s1_it != s1_end && s2_it != s2_end; ++s1_it, ++s2_it) { if (tolower(*s1_it) != tolower(*s2_it)) return false; } return s1_it == s1_end && s2_it == s2_end; } int main(){ std::string s1 = "TotO"; std::string s2 = "ToTO"; if (stricmp(s1, s2)) { std::cout << "s1 = " << s1 << " <=> s2 = " << s2 << std::endl; } return 0; }
Sortie :
s1 = TotO <=> s2 = TOTO
Bonne chance