473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

string operations: looking for a specified letter

8
Hi,
How can I count the the number of times a specific letter like b occurs in a string?
Thank you!!!
Apr 8 '07 #1
23 2492
Ganon11
3,652 Expert 2GB
How do you think you could do it? I could think of several ways.
Apr 8 '07 #2
ria3
8
lol, I have no idea... that's why I posted here :P I need some help!
Apr 8 '07 #3
sandyw
122 100+
I'm a beginner myself.
I would use an if statement to find b's
along with What....
sandy
Apr 8 '07 #4
ria3
8
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.
Apr 8 '07 #5
Ganon11
3,652 Expert 2GB
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.
Apr 9 '07 #6
hirak1984
316 100+
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!!!
Apr 9 '07 #7
JosAH
11,448 Expert 8TB
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
Apr 9 '07 #8
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
Apr 9 '07 #9
JosAH
11,448 Expert 8TB
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
Apr 9 '07 #10
hirak1984
316 100+
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
Apr 9 '07 #11
JosAH
11,448 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. int count= 0;
  2. for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
  3. return count;
kind regards,

Jos
Apr 9 '07 #12
You are right it does save you from working with booleans.

BSCode266
Apr 9 '07 #13
Ganon11
3,652 Expert 2GB
Nope, sorry; there's an overloaded version available. Here's the spoiler:
Expand|Select|Wrap|Line Numbers
  1. int count= 0;
  2. for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
  3. 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...:)
Apr 9 '07 #14
JosAH
11,448 Expert 8TB
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
Apr 9 '07 #15
hirak1984
316 100+
hey thnx buddy that is really tricky.

:D

Nope, sorry; there's an overloaded version available. Here's the spoiler:
Expand|Select|Wrap|Line Numbers
  1. int count= 0;
  2. for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
  3. return count;
kind regards,

Jos
Apr 9 '07 #16
ria3
8
Thank you so much for your help Hirak, BSCode, and JosAH!
I love the tricks on this forum, lol :)
Apr 10 '07 #17
nmadct
83 Expert
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!

Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2. int i = 0;
  3. while (true) {
  4.    i = yourString.indexOf(i);
  5.    if (i == -1)
  6.       break;
  7.    i++;
  8.    count++;
  9. }
  10. return count;
  11.  
This does exactly the same thing but at a glance it's more obvious what it's doing.
Apr 10 '07 #18
nmadct
83 Expert
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.
Apr 10 '07 #19
hirak1984
316 100+
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!

Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2. int i = 0;
  3. while (true) {
  4.    i = yourString.indexOf(i);
  5.    if (i == -1)
  6.       break;
  7.    i++;
  8.    count++;
  9. }
  10. return count;
  11.  
This does exactly the same thing but at a glance it's more obvious what it's doing.
Apr 10 '07 #20
sandyw
122 100+
What did I start the battle of "Counters"!!!
You got to love it,

sandy
Apr 10 '07 #21
JosAH
11,448 Expert 8TB
I would argue that you should NOT think of that... "creative" code is harder to read!

Expand|Select|Wrap|Line Numbers
  1. 1: int count = 0;
  2. 2: int i = 0;
  3. 3: while (true) {
  4. 4:    i = yourString.indexOf(yourChar, i);
  5. 5:    if (i == -1)
  6. 6:       break;
  7. 7:    i++;
  8. 8:    count++;
  9. 9: }
  10.  
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. for(int i= 0 ; (i= yourString.indexOf(yourChar, i)) != -1; i++, count++);
kind regards,

Jos ;-)
Apr 10 '07 #22
prometheuzz
197 Expert 100+
Ha, you people with your multy line solutions!
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @param s     the string to look for
  3.  * @param t     the text to look through for 's'
  4.  * @return      the number of times 's' is in 't'
  5.  */
  6. public static int count(String s, String t) {
  7.   return (t.length()-t.replaceAll(s, "").length())/s.length();
  8. }
; )
Apr 10 '07 #23
JosAH
11,448 Expert 8TB
Ha, you people with your multy line solutions!
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @param s     the string to look for
  3.  * @param t     the text to look through for 's'
  4.  * @return      the number of times 's' is in 't'
  5.  */
  6. public static int count(String s, String t) {
  7.   return (t.length()-t.replaceAll(s, "").length())/s.length();
  8. }
; )
I did expect something to be casted to double though given a gory solution
like that; and then some ...

kind regards,

Jos ;-)
Apr 10 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

22
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. ...
32
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()....
7
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...
9
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...
23
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...
5
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...
5
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 ...
22
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...
9
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
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...
1
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)...
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.