#include #include <cstring> int main() { char s1[32] = "Hello"; char s2[32] = "world"; char* tmp = 0; const char* space = " "; const int size = strlen(s1) + 1 + strlen(s2) + 1; // s1 + ' ' + s2 + \0 tmp = new char[size]; strcpy(tmp, s1); strcat(tmp, space); strcat(tmp, s2); std::cout << "size = " << size << std::endl << "s1 = "" << s1 << """ << std::endl << "s2 = "" << s2 << """ << std::endl << "tmp = "" << tmp << """ << std::endl; delete[] tmp; return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionstd::string a = "Une chaine"; std::string b = "Une chaine"; std::string c = a + " " + b; std::cout << c << std::endl;
#include "stdafx.h" Ouvrage::Ouvrage() { this->code=0; this->auteurs=new char[1]; auteurs[0]=0; this->dateEdition=new char[1]; dateEdition[0]=0; this->titre= new char [1]; titre[0]=0; } Ouvrage::Ouvrage(int code,char *titre,char *auteurs,char*dateEdition) { this->code=code; this->titre=new char[strlen(titre)+1]; strcpy(this->titre,titre); this->auteurs=new char[strlen(auteurs)+1]; strcpy(this->auteurs,auteurs); this->dateEdition=new char[strlen(dateEdition)+1]; strcpy(this->dateEdition,dateEdition); } Ouvrage::Ouvrage(Ouvrage &o) { this->code=o.code; this->titre=new char[strlen(o.titre)+1]; strcpy(this->titre,o.titre); this->auteurs=new char[strlen(o.auteurs)+1]; strcpy(this->auteurs,o.auteurs); this->dateEdition=new char[strlen(o.dateEdition)+1]; strcpy(this->dateEdition,o.dateEdition); } Ouvrage::~Ouvrage() { if (this->auteurs!=NULL) delete [] auteurs; if (this->titre!=NULL) delete [] titre; if (this->dateEdition!=NULL) delete [] dateEdition; } void Ouvrage::print() { cout<<"code:"<<code<<endl; cout<<"titre:"<<titre<<endl; cout<<"date edtion:"<<dateEdition<<endl; cout<<"auteurs:"<titre= new char[strlen(tmp)+1]; strcpy(this->titre,tmp); s=cp; } if(v==2) { tmp=left(info,s+1,cp); this->auteurs= new char[strlen(tmp)+1]; strcpy(this->auteurs,tmp); s=cp; } if(v==3) { tmp=left(info,s+1,cp); this->dateEdition= new char[strlen(tmp)+1]; strcpy(this->dateEdition,tmp); s=cp; } v++; cp++; } } char* Ouvrage::left(char* info,int start,int target) { char *tmp= new char[target-start]; for(int i=start;i<target;i++) tmp[i-start]=info[i]; tmp[target-start]=0; return tmp; } int Ouvrage:: getCode() { return code; } char * Ouvrage::getTitre() { return titre; } char * Ouvrage::getAuteurs() { return auteurs; } char *Ouvrage::getDateEdition() { return dateEdition; } void Ouvrage::setCode(int code) { this->code=code; } void Ouvrage::setTitre(char *titre) { this->titre=new char[strlen(titre)+1]; strcpy(this->titre,titre); } void Ouvrage::Set(Ouvrage o) { this->code=o.code; this->titre=new char[strlen(o.titre)+1]; strcpy(this->titre,o.titre); this->auteurs=new char[strlen(o.auteurs)+1]; strcpy(this->auteurs,o.auteurs); this->dateEdition=new char[strlen(o.dateEdition)+1]; strcpy(this->dateEdition,o.dateEdition); } char * Ouvrage::tostring() { char *tmp; tmp=new char[10]; int a=10; itoa(code,tmp,a); //char *s=new char [strlen(tmp)+strlen(titre)+strlen(auteurs)+strlen(dateEdition)+1]; char *s1=new char [strlen(tmp)+strlen(titre)+1]; //char *s2=new char [strlen(tmp)+strlen(titre)+strlen(auteurs)+1]; char *s3=new char [strlen(tmp)+strlen(titre)+strlen(auteurs)+strlen(dateEdition)+1]; //strcpy(s,tmp && titre && auteurs && dateEdition); // char *titre=new char[strlen(titre)+2]; //titre[strlen(titre)+1]=" "; //titre[strlen(titre)+2]=0; //tmp[strlen(tmp)+2]=" "; s1[0]=0; s3[0]=0; s3=strcat(tmp,titre); //s3[strlen(tmp)+strlen(titre)+1]=0; s3=strcat(s3,auteurs); //s3=strcat(s3,dateEdition); s3[30]=0; cout<<strlen(s3)<<endl; cout<<s3<<endl; cout<<s1<<endl; //cout<<s2<<endl; //cout<<strlen(tmp); //strcpy(s,s3); return s3; } int Ouvrage::FindT(char *titre) { if(strcpy(this->titre,titre)==0){ cout<<"trouver"<<endl;return 1;} else return 0; }
mais moi je dois retourner : le code + le titre + date edition + auteurs dans une seule chaine