M1102 – TP6 – Exercice 11

Dans cet exercice, on souhaite centrer une chaine de caractères sur XXX caractères (XXX étant un entier naturel). Pour cela écrire le corps de la fonction Center () de signature :

string & Center (string & Str, const unsigned long & Width);

Inspirez-vous de ce que vous avez fait pour la fonction TrimRight () (cf. exercice 10).

Ecrire le corps des deux fonctions suivantes :

vector <string> & Center (vector <string> & VString, const unsigned long & Width) ;
vector <string> & Center (vector <string> & VString, const unsigned & LineNumber, const unsigned long & Width) ;

La première centre toutes les lignes du vecteur, la seconde ne s’applique à la ligne LineNumber.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « cen» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 – Exercice 12

Dans cet exercice, on souhaite supprimer tous les espaces en début et en fin de chaine de caractères. Pour cela écrire la fonction Trim () de signature :

string & Trim (string & Str);

Ecrire le corps des deux fonctions suivantes :

vector <string> & Trim (vector <string> & VString) ;
vector <string> & Trim (vector <string> & VString, const unsigned & LineNumber) ;

La première supprime les espaces en début et en fin de toutes les lignes du vecteur, la seconde ne s’applique à la ligne LineNumber.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « tri» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 – Exercice 13

Dans cet exercice, on souhaite justifier une chaine de caractères sur XXX caractères (XXX étant un entier naturel). Pour cela, écrire le corps de la fonction Justify () de signature :

string & Justify (string & Str, const unsigned long & Width);

L’algorithme de haut niveau est le suivant :

  1. Remplacer tous les caractères d’espacement par un espace, et compter combien d’espaces on a à la fin de cet opération.
  2. Substituer (remplacer) chaque caractère d’espacement par le nombre d’espaces nécessaires.

Vue la complexité de l’étape (2), vous pouvez utiliser les fonctions find () et insert () sur les string.

Ecrire le corps des deux fonctions suivantes :

vector <string> & Justify (vector <string> & VString, const unsigned long & Width);
vector <string> & Justify (vector <string> & VString, const unsigned & LineNumber, const unsigned long & Width);

La première justifie toutes les lignes du vecteur, la seconde ne s’applique à la ligne LineNumber.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « jus» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 – Exercice 14

On souhaite annuler une justification sur une chaine de caractères, cad remplacer plusieurs espaces consécutifs par un unique. Pour cela, écrire le corps de la fonction RemoveMultipleSpaces () de signature :

string & RemoveMultipleSpaces (string & Str);

Cette fonction doit appeler la fonction erase () des string pour faire la suppression.

Ecrire le corps des deux fonctions suivantes :

vector <string> & RemoveMultipleSpaces (vector <string> & VString) ;
vector <string> & RemoveMultipleSpaces (vector <string> & VString, const unsigned & LineNumber) ;

La première annule la justification de toutes les lignes du vecteur, la seconde ne s’applique à la ligne LineNumber.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « mul» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 – Exercice 15

Modifier le fichier nsUtil.h en ajoutant la fonction EditVStringV4 () de signature :

void EditVStringV4 (const std::vector <std::string> & VString);

Cette fonction doit afficher à l’écran tout le code ASCII de tous les caractères alphabétiques (majuscules / ou minuscules) de chaque ligne du vecteur VString.

Nb : pour savoir si un caractère est alphabétique, vous devez utilisez la fonction isalpha ().

M1102 – TP6 – Exercice 16

Dans le fichier nsUtil.h, ajouter les deux lignes suivantes :

    const std::string KLeet ("/-\\;|3;(;|);3;|=;9;|-|;1;_|;|<;|_;|\\/|;|\\|;0;|2;Q;|?;$;'][';|_|;\\/;\\/\\/;><;`/;2;");
    //                        a     b c d  e  f g  h  i  j  k  l  m    n    o p  q  r s  t    u  v   w     x   y  z

La première ligne est la correspondance entre notre alphabet et l’alphabet leet, la seconde correspond à notre alphapbet (elle n’est présente que pour vous aidez).

Nb : a l’instar des fichiers CSV, chaque lettre de l’alphabet Lett est séparée par le caractère ';' dans la chaine KLeet.

Ecrire le corps de la fonction ToLett () de signature:

string ToLeet (char c);

Cette fonction prend en paramètre un caractère est renvoie la chaine de caractères correspondante dans l’alphabet Lett.

Ecrire le corps des deux fonctions suivantes :

vector <string> & ToLeet (vector <string> & VString) ;
vector <string> & ToLeet (vector <string> & VString, const unsigned & LineNumber) ;

La première chaque caractère de toutes les lignes du vecteur en son alphabet Leet correspondant, la seconde ne s’applique qu’à la ligne LineNumber.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « lee» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 – Exercice 17

Ecrire les fonctions duales de celles de l‘exercice 16 de façon a transformer une ligne / l’intégralité du vecteur encodé en transformant chaque (chaine de) caractères de l’alphabet Leet en alphabet “standard“.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « unl» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 Corrigés (exo1 – 5)

#include <iostream>
#include <vector>
#include <cctype>
#include <utility>

#include "nsUtil.h"

using namespace std;
using namespace nsUtil;
string & ToUpper (string & Str)
{
    for (char & car : Str)
        car = toupper (car);
    return  Str;
}

vector <string> & ToUpper (vector <string> & VString)
{
    for (string & str : VString)
        str = ToUpper(str);
    return VString;
}

vector <string> & ToUpper (vector <string> & VString, const unsigned & LineNumber)
{
    ToUpper(VString [LineNumber]);
    return VString;
}

string & ToLower (string & Str)
{
    for (char & car : Str)
        car = tolower (car);
    return  Str;
}

vector <string> & ToLower (vector <string> & VString)
{
    for (string & str : VString)
        str = ToLower (str);
    return VString;
}

vector <string> & ToLower (vector <string> & VString, const unsigned & LineNumber)
{
    ToLower (VString [LineNumber]);
    return VString;
}



vector <string> & Swap (vector <string> & VString, const unsigned & PosBeg, const unsigned & PosEnd)
{
    swap (VString[PosBeg], VString[PosEnd]);
    return VString;
}


int main(int argc, char *argv[])
{
    if (2 != argc)
    {
        cerr << "gros boulet" << endl;
        exit (0);
    }

    string InputFile (argv[1]);
    //unsigned long Width (stoul(argv[2]));
    vector <string> VFile (FileToVectString (InputFile));
    EditVStringV3 (VFile);
    string Instruction, Complement;
    while (true)
    {
        cin >> Instruction;
        Instruction = ToLower (Instruction);
        if ("fin" == Instruction)
        {
            break;
        }
        //sauvergarde
        else if ("sav" == Instruction)
        {
            string FileOut;
            cin >> FileOut;
            VectStringToFile (VFile, FileOut);

        }
        //majuscule
        else if ( "maj" ==  Instruction)
        {
            cin >> Complement;
            if (ToLower(Complement) == "tout")
                ToUpper (VFile);
            else
                ToUpper (VFile, stoul(Complement));
        }
        //minuscule
        else if ( "min" ==  Instruction)
        {
            cin >> Complement;
            if (ToLower (Complement) == "tout")
                ToLower (VFile);
            else
                ToLower (VFile, stoul(Complement));
        }

        EditVStringV2 (VFile);
    }//while

    return 0;
}


M1102 – TP4 Corrigés

#include <iostream>
#include <cctype> //isspace ()
#include "EasyAssert.h"
using namespace std;

unsigned CompterDoublonsV1 (const string & Chaine)
{
unsigned Cpt (0);
for (unsigned i (0); i < Chaine.size()-1; i = i + 1)
{
if (isspace(Chaine [i])) continue;
if (Chaine[i] == Chaine [i+1])
{
Cpt = Cpt + 1;
i = i + 1;
}
}
return Cpt;
}//CompterDoublonsV1

void TestCompterDoublonsV1()
{
EASY_ASSERT(CompterDoublonsV1 ("aaaa")==2);
EASY_ASSERT(CompterDoublonsV1 ("aaaaa")==2);
EASY_ASSERT(CompterDoublonsV1 ("aaaaaa")==3);
}//TestCompterDoublonsV1

unsigned CompterDoublonsV2 (const string & Chaine)
{
unsigned Cpt (0);
for (unsigned i (0); i < Chaine.size()-1; i = i + 1)
{
if ((isspace(Chaine [i]) && isspace (Chaine[i + 1])) || (Chaine[i] == Chaine [i+1]))
{
Cpt = Cpt + 1;
}
}
return Cpt;
}//CompterDoublonsV2

void TestCompterDoublonsV2()
{
EASY_ASSERT(CompterDoublonsV2 ("aa  aa")==3);
EASY_ASSERT(CompterDoublonsV2 ("aa  aaa")==4);
EASY_ASSERT(CompterDoublonsV2 ("aa \taa")==3);
}//TestCompterDoublonsV2

unsigned CompterDoublonsV3 (const string & Chaine)
{
unsigned Cpt (0);
char CarPrec (Chaine [0]);
for (unsigned i (0); i < Chaine.size()-1; i = i + 1)
{
if (isspace(Chaine [i])) continue;
if (Chaine[i] == CarPrec)
{
Cpt = Cpt + 1;
}
}
return Cpt;
}//CompterDoublonsV3

void TestCompterDoublonsV3()
{
EASY_ASSERT(CompterDoublonsV2 ("aa  aa")==3);
EASY_ASSERT(CompterDoublonsV2 ("aa  aaa")==4);
EASY_ASSERT(CompterDoublonsV2 ("aa \taa")==3);
}//TestCompterDoublonsV3

unsigned long long TrouveCarDansStr (const char & Caract,
const string & Chaine,
const unsigned long long & Debut)
{
unsigned long long Pos (Debut);
for (; Pos < Chaine.size() && Chaine[Pos] != Caract; Pos = Pos + 1);
return Pos;
}//TrouveCarDansStr ()

unsigned long long FindSubstrInStr(const string & Chaine, const string & SousChaine,
unsigned long long PosDep)
{
if (0 == Chaine.size() || 0 == SousChaine.size() || Chaine.size() + PosDep < SousChaine.size())
return string::npos;
unsigned long long i (TrouveCarDansStr (SousChaine [0], Chaine, PosDep));
while (i < Chaine.size() - SousChaine.size())
{
unsigned long long j (1);
while (j < SousChaine.size() && (Chaine [i + j] == SousChaine [j]))
{
j = j + 1;
}

if (j == SousChaine.size()) return i;
i = TrouveCarDansStr (SousChaine [0], Chaine, i + 1);
}
return string::npos;
}// FindSubstrInStr ()

unsigned long long FindSubstrInStrV2 (const string & Chaine, const string & SousChaine,
unsigned long long PosDep)
{
if (0 == Chaine.size() || 0 == SousChaine.size() || Chaine.size() + PosDep < SousChaine.size())
return string::npos;

for (unsigned long long i (PosDep); i < Chaine.size() - SousChaine.size(); i = i +1)
{
if (Chaine [i] != SousChaine[0]) continue;
unsigned long long j (1);
while (j < SousChaine.size() && (Chaine [i + j] == SousChaine [j]))
{
j = j + 1;
}

if (j == SousChaine.size()) return i;
}
return string::npos;
}//FindSubstrInStrV2 ()

unsigned long long FindSubstrInStrV3 (const string & Chaine, const string & SousChaine,
unsigned long long PosDep)
{
if (0 == Chaine.size() || 0 == SousChaine.size() || Chaine.size() + PosDep < SousChaine.size())
return string::npos;

for (unsigned long long i (PosDep); i < Chaine.size() - SousChaine.size(); i = i +1)
{
if (Chaine.substr(i, SousChaine.size()) == SousChaine) return i;
}
return string::npos;
}//FindSubstrInStrV3 ()

void TestFindSubstrInStr ()
{
string Chaine ("Ma jolie chaine");
string SousChaine ("jolie");
EASY_ASSERT(FindSubstrInStr (Chaine, SousChaine, 0) == 3);
EASY_ASSERT(FindSubstrInStrV2 (Chaine, SousChaine, 0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStrV3 (Chaine, SousChaine, 0));

SousChaine = "pas";
EASY_ASSERT(FindSubstrInStr (Chaine, SousChaine, 0) == string::npos);
EASY_ASSERT(FindSubstrInStrV2 (Chaine, SousChaine, 0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStrV3 (Chaine, SousChaine, 0));
}// TestFindSubstrInStr ()

int main()
{
//TestCompterDoublonsV1 ();
//TestCompterDoublonsV2 ();
//TestCompterDoublonsV3();
TestFindSubstrInStr ();
return 0;
}