Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Function to return the numbers of digits

tokoGtokoG Posts: 209Member
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));

fflush(stdin);
getchar();

}/*end main*/
[/code]


«1

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [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)
  • shaolin007shaolin007 Posts: 1,018Member
    : 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]
    :
    :
    :

  • tsagldtsagld Posts: 621Member
    : 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


  • tokoGtokoG Posts: 209Member
    : [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

    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

  • tokoGtokoG Posts: 209Member
    : : 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.


  • tokoGtokoG Posts: 209Member
    : : 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.


  • shaolin007shaolin007 Posts: 1,018Member
    : : : 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]

  • weapon__xweapon__x Posts: 138Member
    : : 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

  • tokoGtokoG Posts: 209Member
    : : : 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!!
  • LundinLundin Posts: 3,711Member
    : : : : 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

«1
Sign In or Register to comment.