473,729 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting occurence of character in a string

Hi,

Is there a standard library function that counts the number of
occurences of a character in a string?

Regards,

Michael

Nov 23 '05 #1
44 9024
Michael McGarry wrote:

Hi,

Is there a standard library function that counts the number of
occurences of a character in a string?


No.

--
pete
Nov 23 '05 #2
pete wrote:
Michael McGarry wrote:

Hi,

Is there a standard library function that counts the number of
occurences of a character in a string?


No.


If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;
while (*us) if (*us++ == uc) n++;
return n;
}

--
Peter

Nov 23 '05 #3
On 21 Nov 2005 20:30:46 -0800, "Michael McGarry"
<mi************ *@gmail.com> wrote:
Hi,

Is there a standard library function that counts the number of
occurences of a character in a string?

No
<<Remove the del for email>>
Nov 23 '05 #4
Peter Nilsson wrote:
pete wrote:
Michael McGarry wrote:
Hi,

Is there a standard library function that counts the number of
occurences of a character in a string? No.


If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;


return 1 ?
while (*us) if (*us++ == uc) n++;
return n;
}


What's wrong with a simpler version?
size_t strccnt(const char* s, int c)
{
size_t n = 0;

while(*s != '\0') {
if(*s++ == c)
n++;
}

return n;
}
Bjørn
Nov 23 '05 #5
Peter Nilsson wrote:
pete wrote:
Michael McGarry wrote:
Hi,

Is there a standard library function that counts the number of
occurences of a character in a string?


No.

If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;

<snip>

I'd argue for "return 0", or rather, omitting the check altogether. The
NUL character does not occur in the string. That would be more something
for a "memccnt" function.

S.
Nov 23 '05 #6
On 2005-11-22, Skarmander <in*****@dontma ilme.com> wrote:
Peter Nilsson wrote:
pete wrote:
Michael McGarry wrote:

Hi,

Is there a standard library function that counts the number of
occurence s of a character in a string?

No.

If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;

<snip>

I'd argue for "return 0", or rather, omitting the check altogether. The
NUL character does not occur in the string.


Traditionally, the NUL character appears exactly once, at the end of the
string, and this location is returned in strchr/strrchr.
That would be more something for a "memccnt" function.

S.

Nov 23 '05 #7
Jordan Abel wrote:

On 2005-11-22, Skarmander <in*****@dontma ilme.com> wrote:

NUL character does not occur in the string.


Traditionally,
the NUL character appears exactly once, at the end of the
string, and this location is returned in strchr/strrchr.


N869
7. Library
7.1 Introduction
7.1.1 Definitions of terms
[#1] A string is a contiguous sequence of characters
terminated by and including the first null character.

--
pete
Nov 23 '05 #8
Thanks to all for your help.

Nov 23 '05 #9
Jordan Abel wrote:
On 2005-11-22, Skarmander <in*****@dontma ilme.com> wrote:
Peter Nilsson wrote:

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;


<snip>

I'd argue for "return 0", or rather, omitting the check altogether. The
NUL character does not occur in the string.


Traditionally, the NUL character appears exactly once, at the end of the
string, and this location is returned in strchr/strrchr.

You're right. 7.1.1. "A string is a contiguous sequence of characters
terminated by and including the first null character." This means that a
string is considered to include NUL except where stated otherwise. 1 is
the correct answer. (But I'd make it explicit in the function's
description.)

S.
Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2462
by: Tesla | last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a character/string? Like lets say I want to find out the position of the second colon. How do I go about that?? I can only find functions that return the position of the first or last occurence, but not a variable occurence ::sniffles::
4
6571
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle difference between "." and "current()", I found a neat way of doing the same without keys or generate-id(): <xsl:template match="/"> <!-- Selects all "new" elements --> <xsl:for-each select="//Name"> <!-- Display the element -->
10
5306
by: Sean Berry | last post by:
I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I thought I would be able to do it like this: ----------------------------------------------------
1
6927
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
3
1726
by: utab | last post by:
Dear all, I tried to solve the occurence problem: to find the distinct occurences of a word in an input. I know that I could use map and other STD lib functions. I tried to do it the hard way. I tried sth and it is working but I could not structure my algorithm very well so I had to add some more lines after thinking it on a paper draft. I am posting the whole code and waiting different ideas. First I sorted them with the sort function,...
2
1972
by: DaFerg | last post by:
Hi ... I have a weblog database where I want to count the occurences of a table of string values that appear in all the urls viewed. My tblWebLog as a field that contains the url ... tblWebLog. I have another table ... tblStrings ... that has a field for a string value and an integer field to count the occurence of the string in tblWebLog.
4
3091
by: fhmm | last post by:
I am an intermediate programmer and i tried to solve the following programme but with no success the programme is a simple text analyzer that read an input file and cumpute the occurence and percentage of each similar word .for example lets consider the following input file. "The for structure totals the elements of the array one row at the time. The outer for structure begins by setting row of the array to 0." the analysis of the...
4
23599
by: MLH | last post by:
MyString = "All men are created equal" Debug.PrintLen(MyString) Now that's easy. But how about just counting the letter "e"? Or, if I were curious to know how many commas were in the string. How might one do this in a query? Suppose a table had single string field with 100 records, each containg a sentence
1
1563
by: string64 | last post by:
Hi to all.... anyone...how can you count the occurence of a particular character in a string without using any system-defined functions? tnx a lot!!!
0
8932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4537
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2702
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2173
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.