//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*/

: 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 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!!:

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.

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.

: : 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!!: : : : 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.Thanks for the advise, I will change to your suggested style.

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.