# Function to return the numbers of digits

Hi again

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

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

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

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

Thanks!!

[code]
#include
#include

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] != ''; 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() {//[b][red]Debug found on this line!![/b][/red]
char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";

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

fflush(stdin);
getchar();

}/*end main*/
[/code]

• [code]
: } else {//If it's not a digit,
: continue; //Ignore and continue looping to find the next char
[/code]

Delete the above -- superfluous and wrong anyway (you forgot the closing brace after continue)
• : Hi again
:
: I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
:
: [blue][b]int countDigit(char str[]);[/blue][/b]
:
:
: So I wrote the following code using [b]isdigit[/b].
: I think this is fine but, it doesn't execute pointing out that declaration is missing at [b]int main(){[/b] part. No idea why...
:
:
: (**The main function was provided, I just need to create the function [b]countDigit[/b] by using provided function prototype)
:
: Thanks!!
:
: [code]
: #include
: #include
:
: 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] != ''; 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() {//[b][red]Debug found on this line!![/b][/red]
: char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
:
: printf("%d
", countDigit(str));
:
: [red]fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard[/red]
: getchar();
:
: }/*end main*/
: [/code]
:
:
:

• : Hi again
:
: I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
:
: [blue][b]int countDigit(char str[]);[/blue][/b]
:
:
: So I wrote the following code using [b]isdigit[/b].
: I think this is fine but, it doesn't execute pointing out that declaration is missing at [b]int main(){[/b] part. No idea why...
:
:
: (**The main function was provided, I just need to create the function [b]countDigit[/b] 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.

[code]
#include
#include

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

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

printf("%d
", 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] != ''; i++) {
if (isdigit(str[i]))
count++;
}

return count;
}/*end countDigit*/

[/code]

Greets,
Eric Goldstein
www.gvh-maatwerk.nl

• : [code]
: : } else {//If it's not a digit,
: : continue; //Ignore and continue looping to find the next char
: [/code]
:
: Delete the above -- superfluous and wrong anyway (you forgot the closing brace after continue)
:

Hi Stober

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

• : : Hi again
: :
: : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: :
: : [blue][b]int countDigit(char str[]);[/blue][/b]
: :
: :
: : So I wrote the following code using [b]isdigit[/b].
: : I think this is fine but, it doesn't execute pointing out that declaration is missing at [b]int main(){[/b] part. No idea why...
: :
: :
: : (**The main function was provided, I just need to create the function [b]countDigit[/b] by using provided function prototype)
: :
: : Thanks!!
: :
: : [code]
: : #include
: : #include
: :
: : 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] != ''; 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() {//[b][red]Debug found on this line!![/b][/red]
: : char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: :
: : printf("%d
", countDigit(str));
: :
: : [red]fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard[/red]
: : getchar();
: :
: : }/*end main*/
: : [/code]
: :
: :
: :
:
:

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.

• : : Hi again
: :
: : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: :
: : [blue][b]int countDigit(char str[]);[/blue][/b]
: :
: :
: : So I wrote the following code using [b]isdigit[/b].
: : I think this is fine but, it doesn't execute pointing out that declaration is missing at [b]int main(){[/b] part. No idea why...
: :
: :
: : (**The main function was provided, I just need to create the function [b]countDigit[/b] 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.
:
: [code]
: #include
: #include
:
: int countDigit(char str[]); [red]//it is more elegant to put this line into a separate .h file and #include that file[/red]
:
: int main() {//[b][red]Debug found on this line!![/b][/red]
: char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
:
: printf("%d
", 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] != ''; i++) {
: if (isdigit(str[i]))
: count++;
: }
:
: return count;
: }/*end countDigit*/
:
: [/code]
:
: 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.

• : : : Hi again
: : :
: : : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: : :
: : : [blue][b]int countDigit(char str[]);[/blue][/b]
: : :
: : :
: : : So I wrote the following code using [b]isdigit[/b].
: : : I think this is fine but, it doesn't execute pointing out that declaration is missing at [b]int main(){[/b] part. No idea why...
: : :
: : : Would anyone advise?
: : :
: : : (**The main function was provided, I just need to create the function [b]countDigit[/b] by using provided function prototype)
: : :
: : : Thanks!!
: : :
: : : [code]
: : : #include
: : : #include
: : :
: : : 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] != ''; 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() {//[b][red]Debug found on this line!![/b][/red]
: : : char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: : :
: : : printf("%d
", countDigit(str));
: : :
: : : [red]fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard[/red]
: : : getchar();
: : :
: : : }/*end main*/
: : : [/code]
: : :
: : :
: : :
: :
: :
:
: 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.
:
:
:
[green]
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.
[/green]

• : : for (i=0; str[i] != ''; i++) {
// and i suggested you also change this to
: : for (i=0; i < strlen(str); i++) {

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

weaps

• : : : for (i=0; str[i] != ''; 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!!
• : : : : Hi again
: : : :
: : : : I have to implement the following function that returns the number of digit character ('0' to '9') in a string.
: : : :
: : : : [blue][b]int countDigit(char str[]);[/blue][/b]
: : : :
: : : :
: : : : So I wrote the following code using [b]isdigit[/b].
: : : : I think this is fine but, it doesn't execute pointing out that declaration is missing at [b]int main(){[/b] part. No idea why...
: : : :
: : : : Would anyone advise?
: : : :
: : : : (**The main function was provided, I just need to create the function [b]countDigit[/b] by using provided function prototype)
: : : :
: : : : Thanks!!
: : : :
: : : : [code]
: : : : #include
: : : : #include
: : : :
: : : : 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] != ''; 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() {//[b][red]Debug found on this line!![/b][/red]
: : : : char str[] = "ABCDEFGHIJKLMNOPQRSTUVQYXZ1234567890";
: : : :
: : : : printf("%d
", countDigit(str));
: : : :
: : : : [red]fflush(stdin); use rewind(stdin) fflush(stdin) has a undefined behaviour according to the c standard[/red]
: : : : getchar();
: : : :
: : : : }/*end main*/
: : : : [/code]
: : : :
: : : :
: : : :
: : :
: : :
: :
: : 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.
: :
: :
: :
: [green]
: 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.
: [/green]
:
:

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

• : : : 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++)
...
• [b][red]This message was edited by tsagld at 2006-4-24 6:47:51[/red][/b][hr]
And, if I may add, strlen is slow. It has to scan for ''. 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).
• : [b][red]This message was edited by tsagld at 2006-4-24 6:47:51[/red][/b][hr]
: And, if I may add, strlen is slow. It has to scan for ''. 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 [b]time (speed) matters![/b])