Sorell Stratège
Nombre de messages : 521 Localisation : Quel part où le sang coule. Date d'inscription : 01/05/2008
Feuille de personnage Surnom dans la team: Sénéchal Noir Victoire/défaite: (0/0)
| Sujet: TP2 Exercice 2 - Longueur String Lun 26 Nov - 19:19 | |
| - Code:
-
/** * * @file LongueurStrings.cxx * * @author D. Mathieu * * @date 07/12/2011 * **/
#include <iostream> #include <string> #include <iomanip> // setw() #include <algorithm> // swap()
using namespace std;
namespace { void LireStrings ();
void LongueurStrings () { cout << "Longueur d'une chaîne \"tableau de caractères\"" << endl;
for ( ; ; ) { cout << "\nTapez une chaine (\"fin\" pour terminer " "le programme) : ";
string ChaineLue; cin >> ChaineLue;
if ("fin" == ChaineLue) break;
cout << "ChaineLue lue : " << ChaineLue << endl;
cout << "ChaineLue inversée : " << setw (4) << ChaineLue.size() << " - ";
/* Affichage à l'envers "à la volée" */
unsigned i = ChaineLue.length(); while (i > 0) { i = i - 1; cout << ChaineLue [i]; } cout << endl; /* */ /* Inversion dans une nouvelle chaîne */
string ChaineInversee; unsigned j = ChaineLue.length(); while (j > 0) { j = j - 1; ChaineInversee = ChaineInversee + ChaineLue [j]; } cout << ChaineInversee << endl; /* */ /* Inversion de la chaîne initiale */
for (unsigned i = 0, j = ChaineLue.size() - 1; i < ChaineLue.size() / 2; i = i + 1, j = j - 1) { swap (ChaineLue [i], ChaineLue [j]); } cout << ChaineInversee << endl; }
} // LongueurStrings()
} // namespace
int main () { /* * / LireStrings(); /* */
/* */ LongueurStrings(); /* */ return 0;
} // main()
| |
|