#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int nbMot( char* s ){
  if (*s == 0) {
    return 0;
  }
  else {
    if(*s == ' ') 
      return nbMot(s + 1);
    else {
      while (*s != ' ' && *s != 0) {
	s++;
      }
      return 1 + nbMot(s);
    }
  }
}

int taillepremiermot ( char * s) {
  int c=0;
  while (*s != ' ' && *s != 0) {
	s++;
	c++;
  }
  return c;
}
 
/* char ** decoupe ( char * s ) {
  int n = nbMot ( s ) ; int i = 0 , j = 0 , taille = 0 ; char * mc ; //mc=mot courant
  char ** t = malloc ( ( n + 1 ) * sizeof ( char * ) ) ;
  if ( * s == ' ' ) {
    return decoupe ( s + 1 ) ;
  }
  else {
    for ( ; i < n ; i ++ ) {
      taille = taillepremiermot ( s ) ;
      t [ i ] = malloc ( sizeof ( char ) * taille ) ;
      for ( ; j < taille ; j ++ ) {
	t [ i ] [ j ] = s + j ;
      }
      s++;
    }
    t [ n + 1 ] = ' \0 ' ;
  }
  return t;
}

*/



char ** decoupe(char * s){
  int nb_mot = nbMot(s);
  char ** tab = malloc (sizeof(char*)*(nb_mot)+1);
  int i=0;
  int j = 0;
  int t;
  for(;i<nb_mot;i++){
    while (*s == ' ' && *s != 0)
      s++;
    t = taille1ermot(s)+1;
    tab[i]=malloc(sizeof(char)*t);
 
    for(j = 0;j < t - 1;j++){
      tab[i][j]=*s;
      s++;
    }
    tab[i][t - 1]=0; 
  }
  tab[nb_mot] = NULL;
  return tab;
}


int main(int argc, char** argv){

  char * s="b on jour";
  int a=nbMot(s);
  printf("resulat= %d",a);
  int b=taille1ermot(s);
  printf("taille du premie mot : %d",b);
  char ** t=decoupe(s);
  int i = 0 ;
  while ( t [ i ] != '\0' ){
    printf ( "%s \n" , t [ i ] );
    i++ ;
  }
  return 0;

}

