darkchild007 | Bonjour à tous Voila j'ai une petite question pour mon programme java dont le but est d'afficher le nombre d'occurences d'un mot dans une phrase.
Ex:
To be or not to be
Le mot be apparait 2 fois dans la phrase...
Cependant dès de j'insère des espace dans ma phrase rien ne vas plus... Voici ce que j'ai déja.
Code :
- public class recherchemot
- {
- public static void main (String[]args)
- {
- System.out.println ("Entrer une phrase" );
- String phrase= Clavier.lireString();
- System.out.println ("Entrer un mot" );
- String mot= Clavier.lireString();
- System.out.println(" " );
- System.out.println("le mot apparait: " + Nbreoccurs(phrase,mot) + " fois" );
- }
- public static int Nbreoccurs(String phraseParam,String motParam)
- {
- int i=0,j=0,compteur=0,occur=0;
- do
- {
- if (phraseParam.charAt(i)==motParam.charAt(j))
- {
- i++;
- j++;
- compteur++;
- if (compteur==motParam.length())
- {
- occur++;
- compteur=0;
- j=0;
- }
- }
- else
- {
- i++;
- }
- }while(i<phraseParam.length());
- return occur;
- }
- }
|
|