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;
}

M1102 – TP5 – Exercice 3 (Supplémentaire)

Reprenez le premier exercice (peu importe l’approche : mot / mot ou caractère : caractère), mais cette fois-ci, vous devez compter les mots en cherchant les fin de mots.
En conséquence vous devez effectuer un parcours de la fin de la chaine vers le début.

Ecrivez une fonction de test à l’aide de easyassert.

N’oublier de faire des tests avec des chaines vides.