|
 |
|
 |
08-09-2003, 12:34 PM
|
#1 (permalink)
|
|
Masked Moderator
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
|
Character String Number
VB
This function determines how many occurances of a character are in a string.
Code:
Public Function checkString()
Dim strChar As String
Dim Char As String
strChar = "Johnny Boy"
Char = Split(strChar, "y")
return UBound(Char)
'returns 2
End Function
__________________
~Ryan
|
|
|
08-09-2003, 01:21 PM
|
#2 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
now waitaminute,
is this intended as a demostration of the language, or of the api?
your function just calls another function. it doesn't really demonstrate that you are capable of finding the number of times a character appears in a string.
how about we see a reimplementation of instr() in vb instead of just calling it?
for my part, here it is in c:
Code:
int char_count(char *s, char c)
{
int num = 0;
for( ; *s != '\0'; s++)
{
if(*s == c) num++;
}
return num;
}
|
|
|
08-09-2003, 05:20 PM
|
#3 (permalink)
|
|
Masked Moderator
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
|
[quote] Originally posted by joe_bruin
now waitaminute,
is this intended as a demostration of the language, or of the api?
your function just calls another function. it doesn't really demonstrate that you are capable of finding the number of times a character appears in a string.
how about we see a reimplementation of instr() in vb instead of just calling it?
Yes it does.....the instr function is a keyword function in vb. Just like the dateadd one I posted earlier. You can use the instr function outside of another function, just take off the function:
Code:
dim strChar as String = "Johnny Boy"
dim intNum as Integer
intNum = instr(strChar, "y")
'intNum = 2
This is why I like vb so much because I don't need to write 4 or 5 lines of code to do things like this. Why re-invent the wheel.....
__________________
~Ryan
|
|
|
08-09-2003, 05:23 PM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,140
|
I don't know, I think standard libraries are fair game. After all, there's no sense in reinventing the wheel. I know the J2SE implementation of most objects/methods are going to be a lot more efficent than my implementations.
Anyways, Java:
Code:
public int getLength(){
StringTokenizer toks = new StringTokenizer("Johnny Boy", "y");
return toks.countTokens() - 1;
}
Changed it as I didn't notice that the goal was to count "y's" orignially.
|
|
|
08-09-2003, 07:10 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
I am not sure what the goal exactly is so this proggy calculates the amount of characters in a string and counts the occurances of a certain character in a string.
Code:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
string str = "hello johnny";
int i=str.size(); //i=12
int p = count(str.begin(), str.end(), 'h'); //p=2
return 0;
}
__________________
|
|
|
08-09-2003, 10:22 PM
|
#6 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
Re: Character String Number
Quote:
Originally posted by rdove
VB
This function determines if and how many characters are in a string.
|
No it doesn't  it returns the first index in a given string
of the specified character.
__________________
direct entry file specification.
|
|
|
08-10-2003, 07:49 AM
|
#7 (permalink)
|
|
Masked Moderator
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
|
Re: Re: Character String Number
Quote:
Originally posted by npa
No it doesn't it returns the first index in a given string
of the specified character.
|
you know what npa..you are right!!! I dunno what I was thinking...
This returns the number of characters in a string
Code:
dim strChar as String = "Johnny Boy"
dim intNum as Integer
intNum = LEN(strChar)
'intNum = 10
__________________
~Ryan
|
|
|
08-10-2003, 09:30 AM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
And how do you count the number of occurances of a character in a string? 
__________________
|
|
|
08-10-2003, 10:05 AM
|
#9 (permalink)
|
|
Masked Moderator
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
|
Quote:
Originally posted by Valmont
And how do you count the number of occurances of a character in a string?
|
Look at the first post......Thats how you do it in VB....
__________________
~Ryan
|
|
|
08-10-2003, 10:47 AM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
instr(strChar, "y")
This returns (a long/variant) the position of the first occurance of "y".
Not the amount of "y"'s.
__________________
|
|
|
08-10-2003, 11:26 AM
|
#11 (permalink)
|
|
Masked Moderator
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
|
Quote:
Originally posted by Valmont
instr(strChar, "y")
This returns (a long/variant) the position of the first occurance of "y".
Not the amount of "y"'s.
|
Look at the first post.........Happy ????
__________________
~Ryan
|
|
|
08-10-2003, 11:32 AM
|
#12 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
erm... you edited the first post?
I didn't notice 
Sorry mate.
__________________
|
|
|
12-11-2003, 06:09 PM
|
#13 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
Perl makes this so simple it's almost silly.
Code:
sub count_y
{
$string = "Johnny Boy";
$count = ($string =~ tr/y/y/);
return $count;
}
This essentially just replaces every 'y' character with another 'y' character, and returns the total number of replacements to $count. Perl is beautiful.
__________________
--Epsilon--
|
|
|
12-18-2003, 08:46 AM
|
#14 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: NC, USA
Posts: 1
|
Another VB Example:
Code:
Public Function LetterCount(Text As String, Letter As String) As Long
LetterCount = Len(Text) - Len(Replace$(Text, Letter, vbNullString))
End Function
As to the first post, it had errors in my VB environment. Split returns a string array, not a string. Also, Return is not valid in this context; instead, the result should be assigned to the function variable. Unless, this is VB.NET, in which case, the language was not clearly specified.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:45 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|