Hi,
How can I count the the number of times a specific letter like b occurs in a string?
Thank you!!!
23 2492
How do you think you could do it? I could think of several ways.
lol, I have no idea... that's why I posted here :P I need some help!
I'm a beginner myself.
I would use an if statement to find b's
along with What....
sandy
but.. how is that supposed to count it? And I don't know how many are in the string, the program is supposed to count that for me.
Well, you'd have to check each letter to see if it is a b - if it is, you can add 1 to a sum variable, and if not, don't do anything.
well just for hint...
we have a function named chatAt() in java...
try using that.
Hi,
How can I count the the number of times a specific letter like b occurs in a string?
Thank you!!!
If anyone had read the API docs for the String class the indexOf() method
would've shown to be applicable for this job.
Here's a link to the API docs.
kind regards,
Jos
If you look closely in the class String, which obviously i would have done first if i didnt know how to, you will find that there is a method charAt(); this one should help you out, yes this was said before just with a typo in it.
BSCode266
If you look closely in the class String, which obviously i would have done first if i didnt know how to, you will find that there is a method charAt(); this one should help you out, yes this was said before just with a typo in it.
BSCode266
I personally think that the indexOf method is better here, i.e. it keeps your
code simpler at no expense.
kind regards,
Jos
indexOf() definitely helps,but I think it will return the first occurence of the alphabet searched for?
am I correct?
I personally think that the indexOf method is better here, i.e. it keeps your
code simpler at no expense.
kind regards,
Jos
indexOf() definitely helps,but I think it will return the first occurence of the alphabet searched for?
am I correct?
Nope, sorry; there's an overloaded version available. Here's the spoiler: -
int count= 0;
-
for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
-
return count;
kind regards,
Jos
You are right it does save you from working with booleans.
BSCode266
Nope, sorry; there's an overloaded version available. Here's the spoiler: -
int count= 0;
-
for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
-
return count;
kind regards,
Jos
O.o
very clever. I hadn't thought of that. Maybe I need to get more creative with my loops...:)
O.o
very clever. I hadn't thought of that. Maybe I need to get more creative with my loops...:)
I'm just a bag full of tricks ;-)
kind regards,
Jos
hey thnx buddy that is really tricky.
:D
Nope, sorry; there's an overloaded version available. Here's the spoiler: -
int count= 0;
-
for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
-
return count;
kind regards,
Jos
Thank you so much for your help Hirak, BSCode, and JosAH!
I love the tricks on this forum, lol :)
very clever. I hadn't thought of that. Maybe I need to get more creative with my loops...:)
I would argue that you should NOT think of that... "creative" code is harder to read! -
int count = 0;
-
int i = 0;
-
while (true) {
-
i = yourString.indexOf(i);
-
if (i == -1)
-
break;
-
i++;
-
count++;
-
}
-
return count;
-
This does exactly the same thing but at a glance it's more obvious what it's doing.
Not that I'm trying to spoil your cleverness, hehe!
Also, I meant to write: i = yourString.indexOf(charToCount, i);
So it works instead of looping infinitely.
ohk... ohk....
cool down..
no more arguments,plz. :D
I would argue that you should NOT think of that... "creative" code is harder to read! -
int count = 0;
-
int i = 0;
-
while (true) {
-
i = yourString.indexOf(i);
-
if (i == -1)
-
break;
-
i++;
-
count++;
-
}
-
return count;
-
This does exactly the same thing but at a glance it's more obvious what it's doing.
What did I start the battle of "Counters"!!!
You got to love it,
sandy
I would argue that you should NOT think of that... "creative" code is harder to read! -
1: int count = 0;
-
2: int i = 0;
-
3: while (true) {
-
4: i = yourString.indexOf(yourChar, i);
-
5: if (i == -1)
-
6: break;
-
7: i++;
-
8: count++;
-
9: }
-
This does exactly the same thing but at a glance it's more obvious what it's doing.
That's all in the eye of the beholder (I numbered your code lines and fixed the
little typo). The way I think goes like this:
1: and 2: ok initialization stuff
3: ok, an infinite loop
4: huh? ah, got it.
5: 6: double huh? So it isn't in infinite loop after all; so he wanted to say: -
while ((i= yourString.indexOf(yourChar, i)) != -1) ...
7: 8: See? that's the increment part of the loop, so the following would've done
the job: -
for( ; (i= yourString.indexOf(yourChar, i)) != -1; i++, count++);
(staring at the top of the page again: aha! that 'i' variable just has a loop scope
so this is what he meant: -
for(int i= 0 ; (i= yourString.indexOf(yourChar, i)) != -1; i++, count++);
kind regards,
Jos ;-)
Ha, you people with your multy line solutions! - /**
-
* @param s the string to look for
-
* @param t the text to look through for 's'
-
* @return the number of times 's' is in 't'
-
*/
-
public static int count(String s, String t) {
-
return (t.length()-t.replaceAll(s, "").length())/s.length();
-
}
; )
Ha, you people with your multy line solutions! - /**
-
* @param s the string to look for
-
* @param t the text to look through for 's'
-
* @return the number of times 's' is in 't'
-
*/
-
public static int count(String s, String t) {
-
return (t.length()-t.replaceAll(s, "").length())/s.length();
-
}
; )
I did expect something to be casted to double though given a gory solution
like that; and then some ...
kind regards,
Jos ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ineedyourluvin1 |
last post by:
Hello all! I've been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
...
|
by: Wolfgang Draxinger |
last post by:
I understand that it is perfectly possible to store UTF-8 strings
in a std::string, however doing so can cause some implicaions.
E.g. you can't count the amount of characters by length() |
size()....
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have...
|
by: rsine |
last post by:
I have developed a program that sends a command through the serial port
to our business system and then reads from the buffer looking for a
number. Everything worked great on my WinXP system, but...
|
by: comp.lang.tcl |
last post by:
I have a TCL proc that needs to convert what might be a list into a
string to read
consider this:
]; # OUTPUTS Hello World which
is fine for PHP
]; # OUTPUT {{-Hello}}
World, which PHP...
|
by: redamazon200 |
last post by:
I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.
string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";
After the copy str2...
|
by: =?Utf-8?B?YmJkb2J1ZGR5?= |
last post by:
I am having a problem converting string to binary and I am hoping someone can
help me out
I have a sql query that does an update that updates a binary field calles
password
...
|
by: j1mb0jay |
last post by:
I have had to create a simple string encryption program for coursework, I
have completed the task and now have to do a write up on how it could be
improved at a later date. If you could look...
|
by: Ulterior |
last post by:
Hi, everyone,
I have a simple problem, which bothers me for some while. I will try
to explain it -
There is some string, whith different letters in it. Is it possible to
analyse this string...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |