logo

isdigit() funkcija C

Šajā tēmā tiks apspriesta funkcija isdigit () C valodā. Funkcija isdigit () ir iepriekš definēta C bibliotēkas funkcija, ko izmanto, lai pārbaudītu, vai rakstzīme ir ciparu rakstzīme no 0 līdz 9 vai nē. Un, ja dotā rakstzīme ir skaitlisks skaitlis vai cipars, tā atgriež patiesu Būla vērtību vai nulle neatšķiras; pretējā gadījumā tas atgriež nulli vai nepatiesu vērtību. Funkcija isdigit ir deklarēta ctype.h galvenes failā, tāpēc mums ir jāpievieno programma C.

Piemēram, pieņemsim, ka funkcijai isdigit() ievadām rakstzīmi 'h'; funkcija pārbauda, ​​vai ievades rakstzīme ir cipars vai nav. Šeit rakstzīme nav cipars. Tātad funkcija isdigit () atgriež nulli (0) vai nepatiesu vērtību. Līdzīgi mēs atkal ievadām ciparu rakstzīmi '5' funkcijai isdigit(). Šoreiz funkcija atgriež patieso vērtību vai vērtību, kas nav nulle, jo “5” ir ciparu rakstzīme no 0 līdz 9 cipariem.

virkņu masīvs c programmēšanā

Funkcijas isdigit() sintakse

Tālāk ir norādīta funkcijas isdigit () sintakse C valodā.

 int isdigit (int ch); 

Parametri:

Ch - Tas nosaka ciparu rakstzīmi, kas jānodod funkcijā isdigit().

Atgriešanas vērtība:

Funkcija isdigit() pārbauda argumentu “ch”, un, ja ievadītā rakstzīme ir cipars, tā atgriež vērtību, kas nav nulle. Pretējā gadījumā tas parāda nulli vai nepatiesu Būla vērtību.

numpy standartnovirze

1. piemērs: Programma, lai pārbaudītu, vai dotā rakstzīme ir cipars vai nē

Izveidosim programmu, lai pārbaudītu, vai dotās rakstzīmes ir cipars vai neizmanto C programmēšanas funkciju isdigit().

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

Izvade:

 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. 

Iepriekš minētajā programmā mēs definējām dažādas rakstzīmes, piemēram, 'P', '3', '!', '', 0, lai pārbaudītu, vai tās ir derīgas rakstzīmes vai neizmanto funkciju isdigit(). Un tad mēs izmantojam funkciju isdigit(), kas pārbauda un atgriež rakstzīmes 'P', '1', nav cipari.

2. piemērs. Programma, lai pārbaudītu, vai lietotāja ievadītā rakstzīme ir cipars vai nav

Uzrakstīsim programmu, lai noskaidrotu, vai ievades rakstzīme ir derīga, izmantojot funkciju isdigit() valodā C.

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

Izvade:

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

Iepriekš minētajā programmā mēs ievadām lietotāja rakstzīmi “5” un pēc tam izmantojam funkciju isdigit, lai pārbaudītu, vai nodotais arguments ir cipars. Šeit nodotā ​​rakstzīme ir cipars, tāpēc funkcija isdigit() atgriež paziņojumu 'Tas ir cipars'.

python konstruktors

2ndizpildi

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

Līdzīgi, kad funkcijā isdigit() ievadām rakstzīmi “A”, funkcija pārbauda derīgu rakstzīmi, un mēs varam redzēt, ka rakstzīme “A” nav cipars. Tātad funkcija atgriež paziņojumu 'Tas nav cipars.'

3. piemērs. Programma, lai drukātu nulles un nulles skaitļus nokārtotajām rakstzīmēm C

Uzrakstīsim programmu, lai pārbaudītu visas norādītās rakstzīmes, un atgriež nulles un vērtības, kas nav nulles, pamatojoties uz nodotajām rakstzīmēm funkcijai isdigit() valodā C.

komanda chown
 /* 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; } 

Izvade:

 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 

Iepriekš minētajā programmā mēs pārbaudām derīgas dotās ciparu rakstzīmes, izmantojot funkciju isdigit (). Funkcija isdigit() atgriež 1 ciparu rakstzīmēm un 0 programmā definētajiem alfabētiskajiem vai īpašajiem simboliem, kas nav cipari.

4. piemērs: Programma visu masīva elementu pārbaudei, izmantojot funkciju isdigit().

Uzrakstīsim programmu, lai atrastu visus derīgos masīva elementa ciparus, izmantojot C funkciju isdigit().

 #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;>

Mēs deklarējām un inicializējām mainīgo arr [] ar dažiem elementiem iepriekš minētajā programmā. Un tad mēs izveidojam citu masīvu arr2[], kurā ir nulle (0), lai piešķirtu rezultātu tiem elementiem, kas nav derīgi cipari. Funkcija isdigit () pārbauda visus arr[] masīva elementus un derīgajiem ciparu elementiem atgriež vērtības, kas nav nulles. Citādi tas atgriež nulles (0) elementiem, kas nav ciparu.