Comment faire pour compter le nombre de repetition de char dans un
string ? Plus precisement, comment verifier qu'un string ne contient pas
plus de X (par ex X=3) fois le meme char ?
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
Johann Burkard
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un string ?
public boolean hasLessOccurrences(String s, char c, int x) { return getOccurrences(s, c) < x; }
public boolean getOccurrences(String s, char c) { int occ = 0; int l = s.length(); for (int i = 0; i < l; ++i) { if (s.charAt(i) == c) ++occ; } return occ; }
Johann -- also das ding is schwul und scheiss ohne ende...das is meine meinung. ("Zocker Boulette" in )
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un
string ?
public boolean hasLessOccurrences(String s, char c, int x) {
return getOccurrences(s, c) < x;
}
public boolean getOccurrences(String s, char c) {
int occ = 0;
int l = s.length();
for (int i = 0; i < l; ++i) {
if (s.charAt(i) == c) ++occ;
}
return occ;
}
Johann
--
also das ding is schwul und scheiss ohne ende...das is meine meinung.
("Zocker Boulette" in <92664fa6.0210301214.3f461777@posting.google.com>)
Comment faire pour compter le nombre de repetition de char dans un string ?
public boolean hasLessOccurrences(String s, char c, int x) { return getOccurrences(s, c) < x; }
public boolean getOccurrences(String s, char c) { int occ = 0; int l = s.length(); for (int i = 0; i < l; ++i) { if (s.charAt(i) == c) ++occ; } return occ; }
Johann -- also das ding is schwul und scheiss ohne ende...das is meine meinung. ("Zocker Boulette" in )
Fabien
Thierry wrote:
Salut,
Comment faire pour compter le nombre de repetition de char dans un string ? Plus precisement, comment verifier qu'un string ne contient pas plus de X (par ex X=3) fois le meme char ?
Ex : "abac" ok mais "abaca" pas ok
Merci d'avance Th.
Solution de base bourrine :
int cpt=0; for (int i=0;i<str.length();i++) if (str.charAt(i)=='c') cpt++; if (cpt>X) { ... } else {...}
@+ Fabien
Thierry wrote:
Salut,
Comment faire pour compter le nombre de repetition de char dans un
string ? Plus precisement, comment verifier qu'un string ne contient pas
plus de X (par ex X=3) fois le meme char ?
Ex : "abac" ok mais "abaca" pas ok
Merci d'avance
Th.
Solution de base bourrine :
int cpt=0;
for (int i=0;i<str.length();i++) if (str.charAt(i)=='c') cpt++;
if (cpt>X) { ... } else {...}
Comment faire pour compter le nombre de repetition de char dans un string ? Plus precisement, comment verifier qu'un string ne contient pas plus de X (par ex X=3) fois le meme char ?
Ex : "abac" ok mais "abaca" pas ok
Merci d'avance Th.
Solution de base bourrine :
int cpt=0; for (int i=0;i<str.length();i++) if (str.charAt(i)=='c') cpt++; if (cpt>X) { ... } else {...}
@+ Fabien
Fabien
Johann Burkard wrote:
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un string ?
public boolean hasLessOccurrences(String s, char c, int x) { return getOccurrences(s, c) < x; }
public boolean getOccurrences(String s, char c) { int occ = 0; int l = s.length(); for (int i = 0; i < l; ++i) { if (s.charAt(i) == c) ++occ; } return occ; }
Johann
En effet, c'est bcp plus propre...
@+ Fabien
Johann Burkard wrote:
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un
string ?
public boolean hasLessOccurrences(String s, char c, int x) {
return getOccurrences(s, c) < x;
}
public boolean getOccurrences(String s, char c) {
int occ = 0;
int l = s.length();
for (int i = 0; i < l; ++i) {
if (s.charAt(i) == c) ++occ;
}
return occ;
}
Comment faire pour compter le nombre de repetition de char dans un string ?
public boolean hasLessOccurrences(String s, char c, int x) { return getOccurrences(s, c) < x; }
public boolean getOccurrences(String s, char c) { int occ = 0; int l = s.length(); for (int i = 0; i < l; ++i) { if (s.charAt(i) == c) ++occ; } return occ; }
Johann
En effet, c'est bcp plus propre...
@+ Fabien
Stéphane Delpech
"Johann Burkard" a écrit dans le message de news: 41b8a29f$0$29836$
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un string ?
public boolean hasLessOccurrences(String s, char c, int x) { return getOccurrences(s, c) < x; }
public boolean getOccurrences(String s, char c) { int occ = 0; int l = s.length(); for (int i = 0; i < l; ++i) { if (s.charAt(i) == c) ++occ; } return occ; }
Johann --
Attention : "public int getOccurrences(...)" au lieu de "public boolean getOccurrences(...)"
Stéphane
"Johann Burkard" <johannburkard@nexgo.de> a écrit dans le message de news:
41b8a29f$0$29836$9b4e6d93@newsread2.arcor-online.net...
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un
string ?
public boolean hasLessOccurrences(String s, char c, int x) {
return getOccurrences(s, c) < x;
}
public boolean getOccurrences(String s, char c) {
int occ = 0;
int l = s.length();
for (int i = 0; i < l; ++i) {
if (s.charAt(i) == c) ++occ;
}
return occ;
}
Johann
--
Attention : "public int getOccurrences(...)" au lieu de "public boolean
getOccurrences(...)"
"Johann Burkard" a écrit dans le message de news: 41b8a29f$0$29836$
Thierry wrote:
Comment faire pour compter le nombre de repetition de char dans un string ?
public boolean hasLessOccurrences(String s, char c, int x) { return getOccurrences(s, c) < x; }
public boolean getOccurrences(String s, char c) { int occ = 0; int l = s.length(); for (int i = 0; i < l; ++i) { if (s.charAt(i) == c) ++occ; } return occ; }
Johann --
Attention : "public int getOccurrences(...)" au lieu de "public boolean getOccurrences(...)"
Stéphane
Johann Burkard
Stéphane Delpech wrote:
"Johann Burkard" a écrit dans le message de news:
public boolean getOccurrences(String s, char c) {
Attention : "public int getOccurrences(...)" au lieu de "public boolean getOccurrences(...)"
Oui, j'ai pas vu, merci. :-/
Johann -- See the different. (*Tönnes in <a91cm0$105a7p$)
Stéphane Delpech wrote:
"Johann Burkard" <johannburkard@nexgo.de> a écrit dans le message de news:
public boolean getOccurrences(String s, char c) {
Attention : "public int getOccurrences(...)" au lieu de "public boolean
getOccurrences(...)"
Oui, j'ai pas vu, merci. :-/
Johann
--
See the different.
(*Tönnes in <a91cm0$105a7p$2@ID-119467.news.dfncis.de>)