// cString.cpp Utilisations de la classe string // > Initialiser un tableau de string, l'afficher; ajouter un élément // > Convertir un objet 'string' en majuscule // > Trouver la chaîne la plus longue dans le tableau // > Chercher les éléments contenant une sous-cahîne // > Isoler l'extension dans un nom de fichier // > Supprimer espaces en tête et en queue: fonction suppEspaces() // // auteur: R.Astier //---------------------------------------------------------------------- // #include <iostream> #include <string> using namespace std; //---------------------------------------------------------------------- // Supprimer espaces tabulations ... en début et fin de chaîne // par défaut pas de suppression à la fin void suppEspaces(string & ch, int suppEnFin=0) { unsigned int ind = ch.find_first_not_of(" \t\n\r\b"); // Suppression au début ch.erase(0,ind); if(ind == string::npos || !suppEnFin) return; // Suppression à la fin ind = ch.find_last_not_of(" \t\n\r\b"); ch.erase(ind+1,string::npos); } //-------------- Q u e l q u e s u t i l i s a t i o n s ---------- int main( int ntm, char *tm[]) { const int MAXSP=6; // Initialiser un tableau de string string sp[MAXSP]={"FootBall","VoleyBall","Surf"}; unsigned int nsp=3; // nb cases utilisées dans sp unsigned int i; // Affichage du tableau cout <<"Les sports ==> "; for(i=0; i<nsp; i++) cout<<" "<<sp[i]; cout<<endl; // Ajout d'un nouveau sport cout <<"Ajout de HandBall"<<endl; sp[nsp]=string("HandBall"); nsp++; // Convertir une 'string' en majuscule string une=string("Exemple de Texte"); cout<<"Avant: "<<une; for(i=0; i<une.length(); i++) une[i]=toupper(une[i]); cout<<" Après: "<<une<<endl; // Trouver le nom de plus long int imax=0; for( i=1; i<nsp; i++) if(sp[imax].length()<sp[i].length()) imax=i; cout <<"Nom le plus long: " <<sp[imax]<<endl; // Rechercher la sous-chaîne 'Ball' cout<<"Remplacement de 'Ball' ==> "; unsigned int pos; for( i=0; i<nsp; i++) { pos=sp[i].find("Ball"); if(pos != string::npos) { sp[i].replace(pos,4,"BALL"); cout<<" "<<sp[i]; } } cout<<endl; // Chercher l'extension d'un nom de fichier string ext, nf="rep/abc.def.cpp"; unsigned int pp=nf.rfind('.'); cout<<"Extension dans "<<nf<<" ==> "; if(pp == string::npos) ext=""; else ext=nf.substr(pp+1); // on enlève le . cout<<"'"<<ext<<"'"<<endl; // nf="/usr/bin/vim"; cout<<" dans "<<nf<<" ==> "; if(pp == string::npos) ext=""; else ext=nf.substr(pp+1); // on enlève le . cout<<"'"<<ext<<"'"<<endl; // Supprimer les espaces en début de chaîne void suppEspaces(string & ch) string ch; cout<<"Supprimer les espaces"<<endl; ch=" abc "; cout<<" avant: '"<<ch<<"'"; suppEspaces(ch); cout<<" puis: '"<<ch<<"'"<<endl; ch="abc "; cout<<" sans espaces en tête: '"<<ch<<"'"; suppEspaces(ch); cout<<" puis: '"<<ch<<"'"<<endl; ch=" \012"; cout<<" avec tabulation et ret.chariot: '"<<ch<<"'"; suppEspaces(ch); cout<<" puis: '"<<ch<<"'"<<endl; ch=" abc "; cout<<" suppression à la fin: '"<<ch<<"'"; suppEspaces(ch,1); cout<<" puis: '"<<ch<<"'"<<endl; ch="a"; cout<<" suppression à la fin: '"<<ch<<"'"; suppEspaces(ch,1); cout<<" puis: '"<<ch<<"'"<<endl; // Chercher sous-répertoire et nom de fichier // désignation répertoire fichier // /usr/src/chaine.cpp ==> '/usr/src' chaine.cpp // chaine.cpp ==> '.' chaine.cpp // /chaine.cpp ==> '' chaine.cpp // Conversion string ==> tableau de caractères // tab.caractères ==> numérique strtol // numérique ==> string // Lire une chaîne // cout<<"\n> > > "; cin.getline( new char[80],80); return 0; } //fin cString.cpp