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 04-02-2005, 01:34 PM   #1 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
Just wondering...

Is there a way to count the number of times... lets say the letter 'f' appears in a string? If someone knows can you just tell me the function or whatever and explain how to use it? thanks.. (by the way.. i'd want this in C.. i dont kno C++ )

Last edited by destin; 04-02-2005 at 02:00 PM.
destin is offline   Reply With Quote
Old 04-02-2005, 02:31 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Actualy it is a very simple implementation.
Code:
#include <stdio.h>

int count_char(char* str, char needle);

int main()
{
  char line[] = "this is a test in the number of a char is pressent";
  printf("Count of %c in \"%s\" is found to be: %d\n", 'e', line, count_char(line, 'e'));
  return 0;
}

int count_char(char* str, char needle)
{
  int count = 0;
  while(*str)
  {
    if(*str == needle)
      count++;
    str++;
  }
  return count;
}
Altho this requires the string to be terminated with '\0' or NULL, if your string might not be NULL terminated I havn't got an easy one right now.. perhaps some alteration with using strlen() to determan the length befor the runthrough of it all.. But this one will run just as quick as any system command..

Sorry I'm too lazy to check if there even exist one in string.h or any standard library function.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-02-2005, 02:37 PM   #3 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
Oh, thanks. I made my own little program out of this..:
Code:
#include <stdio.h>
#include <conio.h>

int count_char(char* str, char needle);

int main()
{
  char *line, letter;
  
  printf("Count what letter? ");
  letter=getche();
  printf("\r\nIn what string? ");
  gets(line);
  printf("You typed '%c' %d times.", letter, count_char(line, letter));
  return 0;
}

int count_char(char* str, char needle)
{
  int count = 0;
  while(*str)
  {
    if(*str == needle)
      count++;
    str++;
  }
  return count;
}
But i dont understand the count_char() function .. would you mind explaining it?
destin is offline   Reply With Quote
Old 04-02-2005, 02:53 PM   #4 (permalink)
Joel
Registered User
 
Join Date: Mar 2005
Location: Tijuana, BC, México
Posts: 7
Joel is on a distinguished road
Cool.. I also did this one in C:
Code:
#include <stdio.h>

int CountChrs(char* s, char c)
{
    // for move through the 's' array
    int pos = 0;
    // for counting the times of 'c'
    int index = 0;
    // Let the loop begin
    while(s[pos] != '\0')
    {
         // Find if the current char is the same as 'c'
         if (s[pos] == c) index++; // if so... post-increment out var
         pos++;  // if not or it is either... just move one char of the 's'     
    }
    s[pos] = 0;
    // return the times of 'c'
	return index;
}

int main(void)
{
    char c = 'a';
    char str[] = "Calafo";
    printf("Total of %c in %s are: %i\n", c, str, CountChrs(str,c));
	system("pause"); // Not portable
	return 0;
}
Joel is offline   Reply With Quote
Old 04-02-2005, 03:06 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
sure...
Code:
int count_char(char* str, char needle)
Since we're counting chars we ned to be able to call the function with a string (char array) and the char to look for
Code:
int count = 0;
We need to keep track of how many we've found during the count, thus the integer here, if tehres none, it's allready set at 0, since this is strict ANSI C, we need to declare the variabel befor anything else is done in teh function.
Code:
while(*str)
For as long as the string has something besides NULL in it, we should do stuff.
Code:
if(*str == needle)
  count++;
if our current location in the string equals the char we're looking for, increment the counter to reflect our current count.
Code:
str++;
Shift our current location in teh string one storage size to the right, or left, or up, or down, depending on how you like to see it
Code:
return count;
when we exit from teh while loop, we return with our pressent count.

When dealing with strings or char arrays, ther memory usage is actualy consisting of one part beeing the value stored at the possition in teh string aswell as the memory location teh string is occupying.
The *str == needle compares the value stored at current memory location with needle.
The str++ increments the memory value, thus moving us one place further into the memory occupied by teh string, thus possitioning us at the next char in teh string.

Sorry, thats just about the easiest way I can explain it, without draving it up in hand..
Anyway here it is drawn:
Code:
Memory representation of:

char str[] = "string"
           .---.---.---.---.---.---.----.
Index:     | 0 | 1 | 3 | 4 | 5 | 6 | 7  |
           |---|---|---|---|---|---|----|
Char:      |'s'|'t'|'r'|'i'|'n'|'g'|'\0'|
           '---'---'---'---'---'---'----'
mem point:   ^
             '

After str++;

           .---.---.---.---.---.---.----.
Index:     | 0 | 1 | 3 | 4 | 5 | 6 | 7  |
           |---|---|---|---|---|---|----|
Char:      |'s'|'t'|'r'|'i'|'n'|'g'|'\0'|
           '---'---'---'---'---'---'----'
mem point:       ^
                 '
So when you use str[i] you're refering to the index line in the memory, when you address *str you're addressing the char line in the memory, when you use str++, instead of changing the index and looking at the char section from that line, you change the mem point, and see it from that side.

Now the limitation of my ascii skills, prevents me from adding the pointer to next item handling in case 's' is located at memory location 0xff2341de and 't' is located at memory location 0xfff2132d but any compiler logic book, like the dragon book, will tell you all about how this is handlet.. Actualy the thing teh str++ changes is infact the mem pointer shift next item pointer but as I said any compiler designs book will elaborate this to the fullest.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-02-2005, 03:10 PM   #6 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
Ohh.. now i get it.. thanks redhead.
destin is offline   Reply With Quote
Old 04-02-2005, 03:22 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
A few things on your program:
Code:
#include <conio.h>
this is not needed if you think strict ANSI C.

Code:
char *line
...
  gets(line);
This isn't apropriate the line dosn't have any allocated memory to it, and gets() dosnt check for buffer overrun, thus a potential violation and segmentation fault just vaiting to happen.
Code:
#define LENGTH 256
char line[LENGTH+1];
fgets(line, LENGTH, stdin);
is a better and more secure way.
Code:
letter = getche();
isn't any good, it is not cross OS comliant, and is what needs the #include <conio.h> statement.
Code:
letter = (char)fgetc(stdin);
fflush(stdin);
is an ANSI C compliant way, and thus cross OS compliant.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-02-2005, 03:27 PM   #8 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
fgetc?
fflush?
fgets?..lol im not familiar with any of those
destin is offline   Reply With Quote
Old 04-02-2005, 03:29 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
Originally Posted by destin
fgetc?
fflush?
fgets?..lol im not familiar with any of those
Look at what's in stdio.h
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-02-2005, 03:33 PM   #10 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
also.. i came across a problem after replacing my code with yours.. it wouldnt let me input a string after i typed the letter.. replacing
Code:
fgets(line, LENGTH, stdout);
with
Code:
fgets(line, LENGTH, stdin);
fixed it
destin is offline   Reply With Quote
Old 04-02-2005, 03:37 PM   #11 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
yeah I noticed about 1 minut after I posted it, and fixed it.. Just wasn't aware you'd allready snatched my false placed correction.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-02-2005, 03:38 PM   #12 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
lol yeah.. cuz i just checked and it was fixed.. i thought i had copied wrong. hehe
destin is offline   Reply With Quote
Old 04-02-2005, 04:09 PM   #13 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
Now what if i wanted it to count the uppercase/lowercase of that letter too.. for example..
Code:
Count what letter? a
In what string? A dog ate.
You typed 'a' 2 times.
rather than it counting it once for only the a in ate.
destin is offline   Reply With Quote
Old 04-02-2005, 07:21 PM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
it is case sensitive, try it and you'll see.. It is left as an excersise for the viewer to make it case insensitive.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
making a 3 column table dynamically sde PHP 8 08-28-2004 08:14 PM


All times are GMT -8. The time now is 06:39 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