Skip to content

Instantly share code, notes, and snippets.

@sheeit
Created February 14, 2016 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheeit/4bde1e30781f1d124b46 to your computer and use it in GitHub Desktop.
Save sheeit/4bde1e30781f1d124b46 to your computer and use it in GitHub Desktop.
Compter les mots.
/*
* Ce programme compte le nombre des mots dans une phrase.
*/
#include <string.h>
#include <stdio.h>
#define MAX_TABLEAU 100001
/* Function prototypes */
void phraseVersChaine(char tableau[]);
int compterLesMots(char phrase[], int longueur);
int estUneLettre(char caractere);
int main()
{
char phrase[MAX_TABLEAU];
int mots;
phraseVersChaine(phrase);
/* Pour voir si la phrase est correctement sur la chaine phrase[]... */
printf("\nLa phrase est: \"%s\"\n", phrase);
mots = compterLesMots(phrase, strlen(phrase));
printf("\nLa phrase a %d mots.\n", mots);
return 0;
}
/*
* Cette fonction met une phrase entree par l'utilisateur
* dans la chaine de caractere phrase[]
*/
void phraseVersChaine(char str[])
{
int str_len, i, phrase_len;
phrase_len = str_len = MAX_TABLEAU - 1;
printf("\nEntrer une phrase (se termine par \'.\' ou \'!\' ou \'?\')\n");
fgets(str, str_len, stdin);
for (i = 0; i < str_len; ++i)
if ((str[i] == '.') || (str[i] == '!') || (str[i] == '\?')) {
phrase_len = i;
break;
}
printf("\nphrase_len = %d\n", phrase_len);
/* Pour s'arreter a la premiere phrase */
for (i = phrase_len + 1; i < str_len; ++i)
str[i] = '\0';
}
/* Cette fonction compte combien de mots dans la phrase */
int compterLesMots(char str[], int len)
{
int mots = 0, i;
for (i = 0; i < len - 1; ++i) {
if (((str[i + 1] == ' ') || (str[i + 1] == '.')) && (estUneLettre(str[i])))
++mots;
}
return mots;
}
/* Cette function determine si un caractere est une lettre (A-Z or a-z) */
int estUneLettre(char c)
{
int lettre = -1;
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
lettre = 1;
else
lettre = 0;
return lettre;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment