string apprendre; cout << "Pose moi ta question ? "; getline(cin, apprendre); cout << apprendre; string animal[] = { "chat","chien" }; string vehicule[] = { "voiture","moto" }; string nomenclature = animal[0]; //donc nomenclature = chat const int tail2 = sizeof(nomenclature); //tail2 = taille du string chat char mot[tail2]; //le tableau mot[] prend en taille la const tail2 const int tail3 = sizeof(apprendre); //idem pour le text entré par lutilisateur char text[tail3]; int temp = 0; //une premiere boucle pour le mot while (temp < tail2) { mot[temp] = nomenclature[temp]; cout << mot[temp]; temp = temp + 1; } // je n'ai pas fait la 2eme boucle pour le text cin sa beug cout << "la suite du progragam"; char* resultat; resultat = strstr(text, mot); }
std::string str = "There are two needles in this haystack with needles."; std::string str2 = "needle"; std::size_t found = str.find(str2); if (found != std::string::npos) std::cout << "first 'needle' found at: " << found << '\n';
std::vector<int> myvector = {1, 2, 3, 4}; auto it = find(myvector.begin(), myvector.end(), 3); if (it != myvector.end()) std::cout << "Element found in myvector: " << *it << '\n'; else std::cout << "Element not found in myvector\n";
std::vector<std::string> myvector = {"pomme", "poire", "banane", "fraise"}; auto it = find(myvector.begin(), myvector.end(), "banane"); if (it != myvector.end()) std::cout << "Element found in myvector: " << *it << '\n'; else std::cout << "Element not found in myvector\n";
const int tail3 = sizeof(apprendre); //idem pour le text entré par lutilisateur char text[tail3]; int temp = 0; //une premiere boucle pour le mot while (temp < tail2) { mot[temp] = nomenclature[temp]; cout << mot[temp]; temp = temp + 1; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionstring nomenclature = animal[0]; int taill = size(nomenclature); //size() renvoit un int ok cout << taill; //je verifie la valeur 4 taill = taill + 5; //je verifie si c'est bien un int j'additionne cout << taill; // 9 char mot[50]; int temp = 0; while (temp < taill) //est voila mon beug 9 loop { mot[temp] = nomenclature[temp]; //pour un mot nomenclature de 4 donc débordement de tampon en enlevant l'addition tout fonctionne a merveille cout << mot[temp]; temp = temp + 1; }
#include <stdio.h> #include <string.h> #define LIST_SIZE 2 int main() { int i = 0; char word[256]; const char* animal[LIST_SIZE] = { "chat", "chien" }; scanf("%s", word); for (i = 0; i < LIST_SIZE; ++i) { if (strcmp(word, animal[i]) == 0) { printf("Trouvé %s\n", word); } } return 0; }
#include <iostream> #include <vector> int main() { std::string word; std::vector<std::string> animal = { "chat", "chien" }; std::cin >> word; for (auto s : animal) { if (word == s) { std::cout << "Trouvé " << word << std::endl; } } return 0; }
int num= 256; char word[num];
int num= 256; std::vector<char> word(num);
typedef NUM 256 // 0K dans toutes les versions du C const int num = 256; // OK à partir de C99 int num = 256; // Interdit char word[num];
std::string word = "toto";
int num = 256; char* word = malloc(num * sizeof(char)); word[0] = 't'; word[1] = 'o'; word[2] = 't'; word[3] = 'o'; word[4] = '\0'; free(word);
#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> animal = { "chat" }; //marche pas const int taill = size(animal[0]); //const prend la valeur vide d'une variable et beug a la modification cest pas possible avant que le compilateur li la ligne 9 il initialise la ligne 10 en const int en valeur vide ? sa beug je sais pas char tab[taill]; const int num = 4; // ca marche char mot[num]; }
std::vector<std::string> animal = { "chat" };
#include <iostream> #include <string> #include <string> #include <Windows.h> #include <vector> using namespace std; void fonction(char text_c[], char mot_c[]) { int text_initialisation = 0; int timer1 = 0 ; int timer2 = 0 ; int timer3 = 0; int timer4 = 0; char tab_text[1000]; char tab_mot[100]; string text_string = text_c; string mot_string = mot_c; const int x = text_string.length(); const int y = mot_string.length(); while (timer1 < x) { tab_text[timer1] = text_string[timer1]; timer1 = timer1 + 1; if (timer1 == x) { int drop_text = x; drop_text = drop_text + 1; tab_text[drop_text] = '\0'; } } while (timer2 < y) { tab_mot[timer2] = mot_string[timer2]; timer2 = timer2 + 1; if (timer2 == y) { int drop_mot = y; drop_mot = drop_mot + 1; tab_mot[drop_mot] = '\0'; } } while (tab_text[timer4] != '\0') { if (tab_mot[text_initialisation] != tab_text[timer3]) { timer3 = timer3 + 1; if (text_initialisation > 0) { text_initialisation = text_initialisation - 1; } } else { text_initialisation = text_initialisation + 1; timer3 = timer3 + 1; } timer4 = timer4 + 1; if (text_initialisation == y) { cout << "VOUS AVEZ TROUVER UN MOT DANS VOTRE CHAINE DE CHAR LALGORYMTHE : " << text_initialisation << " " << "EST EGAL A LA TAILLE :" << tab_mot ; } } } int main() { char a[] = "chhikhchatkjt"; //text a rentrer char b[] = "chat"; //mot a trouver fonction(a, b); }
#include <iostream> #include <string> #include <vector> using namespace std; void fonction(string a, string b) { int timer = 0; int timer2 = 0; int timer3 = 0; int timer4 = 0; string textstring = a; string motstring = b; int taille_text = size(textstring); int taille_mot = size(motstring); vector<char>text(taille_text); vector<char>mot(taille_mot); while (timer < taille_text) { text[timer] = textstring[timer]; cout << text[timer]; timer = timer + 1; } cout << endl; while (timer2 < taille_mot) { mot[timer2] = motstring[timer2]; cout << mot[timer2]; timer2 = timer2 + 1; } cout << endl; int compteurdebits = 0; while (timer4 < taille_text) { if (mot[compteurdebits] == text[timer3]) { timer3 = timer3 + 1; compteurdebits = compteurdebits + 1; } else { timer3 = timer3 + 1; // donne moi une valeur je corige ma variable petit a petit je ne suis pas une machine j'apprend if (compteurdebits > 0) //meduse sans erreur text_initialisation = 0 { //l'erreur humaine donne une concience a oui je nai pas vu le mot chat trop pres compteurdebits = compteurdebits - 1; } } if (compteurdebits == taille_mot) { cout << "Jai trouver le mot : " << motstring << " dans ta phrase : " << textstring << endl; compteurdebits = 0; } timer4 = timer4 + 1; } } int main() { string a = "la phrase a chercher le mot chat oui la phrase"; //vous pouvez choisir char a[] = "nfekjrzhgekj"; string b = "chat"; //vous pouvez choisir char b[] = "nfekjrzhgekj"; fonction(a, b); }
version pro du code nikel et sans bavure !!!
#include <iostream> #include <string> int mystrstr(const std::string& stack, const std::string& needle) { const int stackSize = stack.size(); const int needleSize = needle.size(); const int shiftSize = stack.size() - needle.size() + 1; for (int shift = 0; shift < shiftSize; ++shift) { int i = 0; while (i + shift < stackSize && i < needleSize && stack[i + shift] == needle[i]) { ++i; } if (i >= needleSize) { return shift; } } return -1; } int main() { std::string a = "la phrase a chercher le mot chat oui la phrase"; std::string b = "chat"; int pos = mystrstr(a, b); if (pos >= 0) { std::cout << "J'ai trouvé le mot: \"" << b << "\" dans la phrase: \"" << a << "\"" << " à la position " << pos << std::endl; } else { std::cout << "Pas trouvé :(" << std::endl; } return 0; }
strstrexiste en C++, c'est
std::search:
int mystrstr(std::string_view stack, std::string_view needle) { auto it = std::search(stack.begin(), stack.end(), std::default_searcher(needle.begin(), needle.end())); if ( it == stack.end() ) return -1; else return std::distance(stack.begin(), it); }