Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-09-2003, 12:34 PM   #1 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
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

rdove is offline   Reply With Quote
Old 08-09-2003, 01:21 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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;
}
joe_bruin is offline   Reply With Quote
Old 08-09-2003, 05:20 PM   #3 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
[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

rdove is offline   Reply With Quote
Old 08-09-2003, 05:23 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,140
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-09-2003, 07:10 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
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;
}
__________________
Valmont is offline   Reply With Quote
Old 08-09-2003, 10:22 PM   #6 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
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.
npa is offline   Reply With Quote
Old 08-10-2003, 07:49 AM   #7 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
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

rdove is offline   Reply With Quote
Old 08-10-2003, 09:30 AM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
And how do you count the number of occurances of a character in a string?
__________________
Valmont is offline   Reply With Quote
Old 08-10-2003, 10:05 AM   #9 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
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

rdove is offline   Reply With Quote
Old 08-10-2003, 10:47 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
instr(strChar, "y")

This returns (a long/variant) the position of the first occurance of "y".
Not the amount of "y"'s.
__________________
Valmont is offline   Reply With Quote
Old 08-10-2003, 11:26 AM   #11 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
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

rdove is offline   Reply With Quote
Old 08-10-2003, 11:32 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
erm... you edited the first post?

I didn't notice
Sorry mate.
__________________
Valmont is offline   Reply With Quote
Old 12-11-2003, 06:09 PM   #13 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
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--
Epsilon is offline   Reply With Quote
Old 12-18-2003, 08:46 AM   #14 (permalink)
Thraxian
Registered User
 
Join Date: Aug 2003
Location: NC, USA
Posts: 1
Thraxian is on a distinguished road
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.
Thraxian is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 01:45 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting