logo

isdigit() funkcija C

Šioje temoje bus aptariama funkcija isdigit() C kalba. Funkcija isdigit () yra iš anksto nustatyta C bibliotekos funkcija, kuri naudojama patikrinti, ar simbolis yra skaitinis simbolis nuo 0 iki 9, ar ne. Ir jei nurodytas simbolis yra skaitinis skaičius arba skaitmuo, jis grąžina tikrą Būlio reikšmę arba ne nulį; kitu atveju jis grąžina nulinę arba klaidingą vertę. Funkcija isdigit yra deklaruojama ctype.h antraštės faile, todėl turime pridėti prie C programos.

Pavyzdžiui, tarkime, kad į funkciją isdigit() įvedame simbolį 'h'; funkcija patikrina, ar įvesties simbolis yra skaitmuo, ar ne. Čia simbolis nėra skaitmuo. Taigi funkcija isdigit () grąžina nulį (0) arba klaidingą reikšmę. Panašiai į funkciją isdigit () vėl įrašome skaitinį simbolį „5“. Šį kartą funkcija grąžina tikrąją arba nulinę reikšmę, nes „5“ yra skaitinis simbolis nuo 0 iki 9 skaitmenų.

string concat java

Funkcijos isdigit () sintaksė

Toliau pateikiama funkcijos isdigit () sintaksė C kalba.

 int isdigit (int ch); 

Parametrai:

Ch - Jis apibrėžia skaitmeninį simbolį, kuris turi būti perduodamas naudojant funkciją isdigit().

Grąžinimo vertė:

Funkcija isdigit() patikrina argumentą „ch“ ir, jei perduotas simbolis yra skaitmuo, ji grąžina nulinę reikšmę. Kitu atveju rodoma nulinė arba klaidinga Būlio reikšmė.

kas yra sąsaja

1 pavyzdys: Programa, skirta patikrinti, ar nurodytas simbolis yra skaitmuo, ar ne

Sukurkime programą, kuri patikrintų, ar pateikti simboliai yra skaitmenys, ar ne, naudodami funkciją isdigit() C programavimo programoje.

 /* Check whether the given characters are digits or not in C. */ #include #include int main () { // Use the isdigit() function to check the character is digit or not. */ if (isdigit ( 'P' ) == 0) //check 'P' is digit { printf (' 
 The given character 'P' is not digit'. '); } else { printf ('
 The given character 'P' is a digit. '); } if (isdigit ( '3' ) == 0) //check for '3' { printf (' 
 The given character '3' is not digit'. '); } else { printf ('
 The given character '3' is a digit. '); } if (isdigit ( '!' ) == 0) //check for '!' character { printf (' 
 The given character '!' is not digit'. '); } else { printf ('
 The given character '!' is a digit. '); } if (isdigit ( '' ) == 0) //check for '44' { printf (' 
 The given character '' is not digit'. '); } else { printf ('
 The given character '' is a digit. '); } if (isdigit ( '0' ) == 0) //check for '0' { printf (' 
 The given character '0' is not a digit. '); } else { printf ('
 The given character '0' is a digit. '); } return 0; } 

Išvestis:

 The given character 'P' is not a digit. The given character '3' is a digit. The given character '!' is not a digit. The given character '' is not a digit. The given character '0' is not a digit. 

Aukščiau pateiktoje programoje apibrėžėme skirtingus simbolius, pvz., „P“, „3“, „!“, „“, 0, kad patikrintume, ar tai tinkami simboliai, ar nenaudojant funkcijos isdigit(). Tada mes naudojame funkciją isdigit (), kuri patikrina ir grąžina simbolius „P“, „1“, nėra skaitmenys.

2 pavyzdys: Programa, skirta patikrinti, ar vartotojo įvestas simbolis yra skaitmuo, ar ne

Parašykime programą, kad sužinotume, ar įvesties simbolis tinkamas, ar ne, naudodami funkciją isdigit() C.

java rūšiavimo sąrašas
 /* Check whether the given characters are digits or not in C. */ #include #include int main () { char n; // declare an integer type variable n printf (' Enter a number to check for valid digits: '); scanf ('%c', &n); // use the isdigit() function to check the digit if (isdigit (n)) { printf (' 
 It is a digit. '); } else { printf (' 
 It is not a digit. '); } return 0; } 

Išvestis:

 Enter a number to check for valid digits: 5 It is a digit. 

Aukščiau pateiktoje programoje įvedame vartotojo simbolį „5“, o tada naudojame funkciją „isdigit“, kad patikrintume, ar pateiktas argumentas yra skaitmuo. Čia perduotas simbolis yra skaitmuo, todėl funkcija isdigit() grąžina teiginį „Tai yra skaitmuo“.

2ndegzekucija

 Enter a number to check for valid digits: A It is not a digit. 

Panašiai, kai įvedame simbolį „A“ į funkciją isdigit (), funkcija patikrina, ar simbolis yra tinkamas, ir matome, kad simbolis „A“ nėra skaitmuo. Taigi funkcija grąžina teiginį „Tai nėra skaitmuo“.

3 pavyzdys: programa, skirta spausdinti nulinius ir nulinius skaičius, kai perduodamas simbolis C

Parašykime programą, kuri patikrintų visus duotus simbolius ir grąžintų nulių ir nulių reikšmes pagal perduodamus simbolius funkcijai isdigit() C.

sąrašą rūšiuoti java
 /* create a simple program to return zeroes and non-zeroes values based on the given characters. */ #include #include int main () { char num; // declare num as the character type variable num = '0'; // check character 0 using the isdigit() function printf (' 
 The isdigit() function returns result based on character (0) passed: %d ', isdigit (num)); num = 'o'; // check character o using the isdigit() function printf (' 
 The isdigit() function returns result based on character (o) passed: %d ', isdigit (num)); num = '08'; // check character 08 using the isdigit() function printf (' 
 The isdigit() function returns result based on character (08) passed: %d ', isdigit (num)); num = '+'; // check character + using the isdigit() function printf (' 
 The isdigit() function returns result based on character (+) passed: %d ', isdigit (num)); num = '%'; // check character % using the isdigit() function printf (' 
 The isdigit() function returns result based on character ('%') passed: %d ', isdigit (num)); return 0; } 

Išvestis:

 The isdigit() function returns result based on character (0) passed: 1 The isdigit() function returns result based on character (o) passed: 0 The isdigit() function returns result based on character (08) passed: 1 The isdigit() function returns result based on character (+) passed: 0 The isdigit() function returns result based on character (%) passed: 0 

Aukščiau pateiktoje programoje mes tikriname, ar yra tinkamų skaitmenų simbolių, naudodami funkciją isdigit (). Funkcija isdigit() grąžina 1 į skaitinius simbolius ir 0 į programoje apibrėžtus abėcėlės arba specialiuosius simbolius, kurie nėra skaitmenys.

4 pavyzdys: programa, skirta patikrinti visus masyvo elementus naudojant funkciją isdigit().

Parašykime programą, kuri rastų visus galiojančius masyvo elemento skaitmenis, naudodami funkciją isdigit() C.

 #include #include #include int main () { int i; // declare and initialize the character type array char arr[8] = {&apos;E&apos;, &apos;08&apos;, &apos;@&apos;, &apos;-&apos;, &apos;3&apos;, &apos;/&apos;, &apos;007&apos;, &apos;$&apos;}; int arr2[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // initialize arr2 with containing all zeroes element // use for loop to iterate all elements for (i = 0; i <8; i++) { check and assign digits to the arr2[i] using isdigit() function (arr[i]); } for (i="0;" i < 8; display array characters printf (' 
 '%c'', arr[i]); elements if (arr2[i] !="0)" is a digit character. '); else not valid return 0; pre> <p> <strong>Output:</strong> </p> <pre> &apos;E&apos; is not a valid digit character. &apos;8&apos; is a digit character. &apos;@&apos; is not a valid digit character. &apos;-&apos; is not a valid digit character. &apos;3&apos; is a digit character. &apos;/&apos; is not a valid digit character. &apos;7&apos; is a digit character. &apos;$&apos; is not a valid digit character. </pre> <p>We declared and initialized the arr[] variable with some elements in the above program. And then, we create another array arr2[] that contains zero (0) to assign the result to those elements that are not valid digits. The isdigit() function checks all the arr[] array elements and returns non-zero values to the valid digit elements. Else it returns zeroes (0) to non-digit elements.</p> <hr></8;>

Mes deklaravome ir inicijavome arr [] kintamąjį su kai kuriais aukščiau pateiktos programos elementais. Tada sukuriame kitą masyvą arr2[], kuriame yra nulis (0), kad priskirtume rezultatą tiems elementams, kurie nėra tinkami skaitmenys. Funkcija isdigit () patikrina visus arr[] masyvo elementus ir grąžina ne nulines reikšmes į galiojančius skaitmenų elementus. Kitu atveju jis grąžina nulius (0) neskaitmeniniams elementams.