C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Function to return the numbers of digits Posted by tokoG on 19 Apr 2006 at 12:05 AM
Hi again

I have to implement the following function that returns the number of digit character ('0' to '9') in a string.

int countDigit(char str[]);[/b]


So I wrote the following code using isdigit.
I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...

Would anyone advise?

(**The main function was provided, I just need to create the function countDigit by using provided function prototype)

Thanks!!

#include <stdio.h>
#include <ctype.h>

int countDigit(char str[]) {
  int i;//For-Loop counter
  int count = 0;//Initialise the counter to zero

  //Search through the string str[] to find ho many digits it contains
  for (i=0; str[i] != '\0'; i++) {
   if (isdigit(str[i])) {//If it is a digit in the string str[],
     count++;//increase the count = counter show the number of digit
   } else {//If it's not a digit,
       continue; //Ignore and continue looping to find the next char or digit
  }/*end for*/

  return count;
}/*end countDigit*/

int main() {//Debug found on this line!![/red]
  char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";

  printf("%d\n", countDigit(str));

  fflush(stdin);
  getchar();

}/*end main*/



Report
Re: Function to return the numbers of digits Posted by stober on 19 Apr 2006 at 2:26 AM
:    } else {//If it's not a digit,
:        continue; //Ignore and continue looping to find the next char 


Delete the above -- superfluous and wrong anyway (you forgot the closing brace after continue)
Report
Re: Function to return the numbers of digits Posted by tokoG on 20 Apr 2006 at 7:58 PM
:
: :    } else {//If it's not a digit,
: :        continue; //Ignore and continue looping to find the next char 
: 

:
: Delete the above -- superfluous and wrong anyway (you forgot the closing brace after continue)
:

Hi Stober

Thank you for the advise.
I deleted it and it works fine now.
It outpust the correct number of the digit containing in the string. (Which is 10)

It's my misunderstanding of if condition and if-else condition.

TOKO

Report
Re: Function to return the numbers of digits Posted by shaolin007 on 19 Apr 2006 at 4:58 AM
: Hi again
:
: I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
:
: int countDigit(char str[]);[/b]
:
:
: So I wrote the following code using isdigit.
: I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...
:
: Would anyone advise?
:
: (**The main function was provided, I just need to create the function countDigit by using provided function prototype)
:
: Thanks!!
:
:
: #include <stdio.h>
: #include <ctype.h>
: 
: int countDigit(char str[]) {
:   int i;//For-Loop counter
:   int count = 0;//Initialise the counter to zero
: 
:   //Search through the string str[] to find ho many digits it contains
:   for (i=0; str[i] != '\0'; i++) {
:    if (isdigit(str[i])) {//If it is a digit in the string str[],
:      count++;//increase the count = counter show the number of digit
:    } else {//If it's not a digit,
:        continue; //Ignore and continue looping to find the next char or digit
:   }/*end for*/
: 
:   return count;
: }/*end countDigit*/
: 
: int main() {//Debug found on this line!![/red]
:   char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: 
:   printf("%d\n", countDigit(str));
: 
:   fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard
:   getchar();
: 
: }/*end main*/
: 

:
:
:

Report
Re: Function to return the numbers of digits Posted by tokoG on 20 Apr 2006 at 8:00 PM
: : Hi again
: :
: : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: :
: : int countDigit(char str[]);[/b]
: :
: :
: : So I wrote the following code using isdigit.
: : I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...
: :
: : Would anyone advise?
: :
: : (**The main function was provided, I just need to create the function countDigit by using provided function prototype)
: :
: : Thanks!!
: :
: :
: : #include <stdio.h>
: : #include <ctype.h>
: : 
: : int countDigit(char str[]) {
: :   int i;//For-Loop counter
: :   int count = 0;//Initialise the counter to zero
: : 
: :   //Search through the string str[] to find ho many digits it contains
: :   for (i=0; str[i] != '\0'; i++) {
: :    if (isdigit(str[i])) {//If it is a digit in the string str[],
: :      count++;//increase the count = counter show the number of digit
: :    } else {//If it's not a digit,
: :        continue; //Ignore and continue looping to find the next char or digit
: :   }/*end for*/
: : 
: :   return count;
: : }/*end countDigit*/
: : 
: : int main() {//Debug found on this line!![/red]
: :   char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: : 
: :   printf("%d\n", countDigit(str));
: : 
: :   fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard
: :   getchar();
: : 
: : }/*end main*/
: : 

: :
: :
: :
:
:

Hi Shaolin

I was advised that from this msg board, I think it was you.
I don't normally use fflush(stdin) since then but this main function (which includes fflush(stdin) to hold the screen) was provided as the template so I couldn't change. ;)

Thanks tho.


Report
Re: Function to return the numbers of digits Posted by shaolin007 on 21 Apr 2006 at 5:52 PM
: : : Hi again
: : :
: : : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: : :
: : : int countDigit(char str[]);[/b]
: : :
: : :
: : : So I wrote the following code using isdigit.
: : : I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...
: : :
: : : Would anyone advise?
: : :
: : : (**The main function was provided, I just need to create the function countDigit by using provided function prototype)
: : :
: : : Thanks!!
: : :
: : :
: : : #include <stdio.h>
: : : #include <ctype.h>
: : : 
: : : int countDigit(char str[]) {
: : :   int i;//For-Loop counter
: : :   int count = 0;//Initialise the counter to zero
: : : 
: : :   //Search through the string str[] to find ho many digits it contains
: : :   for (i=0; str[i] != '\0'; i++) {
: : :    if (isdigit(str[i])) {//If it is a digit in the string str[],
: : :      count++;//increase the count = counter show the number of digit
: : :    } else {//If it's not a digit,
: : :        continue; //Ignore and continue looping to find the next char or digit
: : :   }/*end for*/
: : : 
: : :   return count;
: : : }/*end countDigit*/
: : : 
: : : int main() {//Debug found on this line!![/red]
: : :   char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: : : 
: : :   printf("%d\n", countDigit(str));
: : : 
: : :   fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard
: : :   getchar();
: : : 
: : : }/*end main*/
: : : 

: : :
: : :
: : :
: :
: :
:
: Hi Shaolin
:
: I was advised that from this msg board, I think it was you.
: I don't normally use fflush(stdin) since then but this main function (which includes fflush(stdin) to hold the screen) was provided as the template so I couldn't change. ;)
:
: Thanks tho.
:
:
:

No problem, I use to use that same method. rewind(stdin) seems to be a safe bet but I even found someone on the internet who said that wouldn't work either but I find no mention of that in the c standard though. If anyone can or has a better work around I would appreciate to see it.


Report
Re: Function to return the numbers of digits Posted by Lundin on 24 Apr 2006 at 5:52 AM
: : : : Hi again
: : : :
: : : : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: : : :
: : : : int countDigit(char str[]);[/b]
: : : :
: : : :
: : : : So I wrote the following code using isdigit.
: : : : I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...
: : : :
: : : : Would anyone advise?
: : : :
: : : : (**The main function was provided, I just need to create the function countDigit by using provided function prototype)
: : : :
: : : : Thanks!!
: : : :
: : : :
: : : : #include <stdio.h>
: : : : #include <ctype.h>
: : : : 
: : : : int countDigit(char str[]) {
: : : :   int i;//For-Loop counter
: : : :   int count = 0;//Initialise the counter to zero
: : : : 
: : : :   //Search through the string str[] to find ho many digits it contains
: : : :   for (i=0; str[i] != '\0'; i++) {
: : : :    if (isdigit(str[i])) {//If it is a digit in the string str[],
: : : :      count++;//increase the count = counter show the number of digit
: : : :    } else {//If it's not a digit,
: : : :        continue; //Ignore and continue looping to find the next char or digit
: : : :   }/*end for*/
: : : : 
: : : :   return count;
: : : : }/*end countDigit*/
: : : : 
: : : : int main() {//Debug found on this line!![/red]
: : : :   char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: : : : 
: : : :   printf("%d\n", countDigit(str));
: : : : 
: : : :   fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard
: : : :   getchar();
: : : : 
: : : : }/*end main*/
: : : : 

: : : :
: : : :
: : : :
: : :
: : :
: :
: : Hi Shaolin
: :
: : I was advised that from this msg board, I think it was you.
: : I don't normally use fflush(stdin) since then but this main function (which includes fflush(stdin) to hold the screen) was provided as the template so I couldn't change. ;)
: :
: : Thanks tho.
: :
: :
: :
:
: No problem, I use to use that same method. rewind(stdin) seems to be a safe bet but I even found someone on the internet who said that wouldn't work either but I find no mention of that in the c standard though. If anyone can or has a better work around I would appreciate to see it.
:

:
:


It is explicitly written in ANSI C that using fflush() on input streams is undefined behaviour. However, the standard doesn't mention any reason why you couldn't use rewind() or fseek() on an input buffer.

But if you do so, you may leave garbage in the OS's internal buffers.
Or so they say at comp.lang.c: http://c-faq.com/stdio/stdinflush2.html

Report
Re: Function to return the numbers of digits Posted by tsagld on 20 Apr 2006 at 12:01 AM
: Hi again
:
: I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
:
: int countDigit(char str[]);[/b]
:
:
: So I wrote the following code using isdigit.
: I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...
:
: Would anyone advise?
:
: (**The main function was provided, I just need to create the function countDigit by using provided function prototype)
:
: Thanks!!
:

The code you posted should work. But if the countDigit() functions is defined below the main() function you will get a compilation error, because at the point where main() calls the function, the compiler doesn't know it yet.

It is better to always declare the function prototype first, preferably in a separate header file.

#include <stdio.h>
#include <ctype.h>

int countDigit(char str[]); //it is more elegant to put this line into a separate .h file and #include that file

int main() {//Debug found on this line!![/red]
  char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";

  printf("%d\n", countDigit(str));

  fflush(stdin);
  getchar();

}/*end main*/

int countDigit(char str[]) {
  int i;//For-Loop counter
  int count = 0;//Initialise the counter to zero
 
  //Search through the string str[] to find ho many digits it contains
  for (i=0; str[i] != '\0'; i++) {
   if (isdigit(str[i]))
     count++;
   }
 
  return count;
}/*end countDigit*/



Greets,
Eric Goldstein
www.gvh-maatwerk.nl


Report
Re: Function to return the numbers of digits Posted by tokoG on 20 Apr 2006 at 8:01 PM
: : Hi again
: :
: : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: :
: : int countDigit(char str[]);[/b]
: :
: :
: : So I wrote the following code using isdigit.
: : I think this is fine but, it doesn't execute pointing out that declaration is missing at int main(){ part. No idea why...
: :
: : Would anyone advise?
: :
: : (**The main function was provided, I just need to create the function countDigit by using provided function prototype)
: :
: : Thanks!!
: :
:
: The code you posted should work. But if the countDigit() functions is defined below the main() function you will get a compilation error, because at the point where main() calls the function, the compiler doesn't know it yet.
:
: It is better to always declare the function prototype first, preferably in a separate header file.
:
:
: #include <stdio.h>
: #include <ctype.h>
: 
: int countDigit(char str[]); //it is more elegant to put this line into a separate .h file and #include that file
: 
: int main() {//Debug found on this line!![/red]
:   char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: 
:   printf("%d\n", countDigit(str));
: 
:   fflush(stdin);
:   getchar();
: 
: }/*end main*/
: 
: int countDigit(char str[]) {
:   int i;//For-Loop counter
:   int count = 0;//Initialise the counter to zero
:  
:   //Search through the string str[] to find ho many digits it contains
:   for (i=0; str[i] != '\0'; i++) {
:    if (isdigit(str[i]))
:      count++;
:    }
:  
:   return count;
: }/*end countDigit*/
: 
: 

:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:
:
:

Yes it does look smarter. :)
I like the style of having the function prototypes in front of main and bring the main on top of any function, too.
Thanks for the advise, I will change to your suggested style.


Report
Re: Function to return the numbers of digits Posted by weapon__x on 22 Apr 2006 at 3:08 PM
: : for (i=0; str[i] != '\0'; i++) {
// and i suggested you also change this to
: : for (i=0; i < strlen(str); i++) {

it happens....
baboy... baboy...

weaps

Report
Re: Function to return the numbers of digits Posted by tokoG on 22 Apr 2006 at 7:54 PM
: : : for (i=0; str[i] != '\0'; i++) {
: // and i suggested you also change this to
: : : for (i=0; i < strlen(str); i++) {
:
: it happens....
: baboy... baboy...
:
: weaps
:
:

I like that code better too.
Thanks!!
Report
Re: Function to return the numbers of digits Posted by Lundin on 24 Apr 2006 at 5:57 AM
: : : for (i=0; i < strlen(str); i++) {


At first glance, it might look cleaner. But what it actually does is to call strlen() every round in the loop. Not very effective.
Either keep the code as it was, or do like this:

int len = strlen(str);
for(i=0; i < len; i++)
...
Report
Re: Function to return the numbers of digits Posted by tsagld on 24 Apr 2006 at 6:47 AM
This message was edited by tsagld at 2006-4-24 6:47:51

And, if I may add, strlen is slow. It has to scan for '\0'. The larger the string, the slower it is.
(as opposed to languages that use BSTR's, where the length is retained in memory, preceding the string. VB6 and .Net based languages are examples of these).
Report
Re: Function to return the numbers of digits Posted by tokoG on 24 Apr 2006 at 8:08 PM
: This message was edited by tsagld at 2006-4-24 6:47:51

: And, if I may add, strlen is slow. It has to scan for '\0'. The larger the string, the slower it is.
: (as opposed to languages that use BSTR's, where the length is retained in memory, preceding the string. VB6 and .Net based languages are examples of these).
:

Lundin & Eric

OK, noted, thanks for the advices.
(I am just learning thru the search & sort part that where the time (speed) matters!)






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.