logo

isdigit()-funktionen i C

Detta ämne kommer att diskutera funktionen isdigit() i C-språket. Funktionen isdigit() är en fördefinierad funktion i C-biblioteket, som används för att kontrollera om tecknet är ett numeriskt tecken från 0 till 9 eller inte. Och om det givna tecknet är ett numeriskt tal eller siffra, returnerar det ett sant booleskt värde eller icke-noll; annars returnerar den noll eller falskt värde. Funktionen isdigit deklareras i rubrikfilen ctype.h, så vi måste lägga till i C-programmet.

Anta till exempel att vi anger tecknet 'h' i funktionen isdigit(); funktionen kontrollerar om det inmatade tecknet är en siffra eller inte. Här är tecknet inte en siffra. Så funktionen isdigit() returnerar noll (0) eller falskt värde. På samma sätt anger vi återigen det numeriska tecknet '5' till isdigit()-funktionen. Denna gång returnerar funktionen ett sant eller icke-noll värde eftersom '5' är ett numeriskt tecken från 0 till 9 siffror.

intervjufrågor för java språk

Syntax för isdigit()-funktionen

Följande är syntaxen för funktionen isdigit() i C-språket.

 int isdigit (int ch); 

Parametrar:

Ch - Den definierar det numeriska tecknet som ska skickas i funktionen isdigit().

Returvärde:

Funktionen isdigit() kontrollerar 'ch'-argumentet, och om det godkända tecknet är en siffra, returnerar det ett värde som inte är noll. Annars visar den ett noll eller falskt booleskt värde.

tkinter-knappen

Exempel 1: Program för att kontrollera om det givna tecknet är siffra eller inte

Låt oss skapa ett program för att kontrollera att de givna tecknen är siffror eller inte med funktionen isdigit() i C-programmeringen.

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

Produktion:

 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. 

I programmet ovan definierade vi olika tecken som 'P', '3', '!', '', 0 för att kontrollera om dessa är giltiga tecken eller inte med isdigit()-funktionen. Och sedan använder vi funktionen isdigit() som kontrollerar och returnerar tecknen 'P', '1', är inte siffror.

Exempel 2: Program för att kontrollera om tecknet som angetts av användaren är en siffra eller inte

Låt oss skriva ett program för att ta reda på om inmatningstecknet är giltigt eller inte med funktionen isdigit() i C.

arv i 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; } 

Produktion:

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

I programmet ovan matar vi in ​​tecknet '5' från användaren och använder sedan isdigit-funktionen för att kontrollera om det godkända argumentet är en siffra. Här är det godkända tecknet en siffra, så funktionen isdigit() returnerar påståendet 'Det är en siffra.'

2ndavrättning

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

På liknande sätt, när vi anger tecknet 'A' i isdigit()-funktionen, söker funktionen efter det giltiga tecknet, och vi kan se att tecknet 'A' inte är en siffra. Så, funktionen returnerar påståendet 'Det är inte en siffra.'

Exempel 3: Program för att skriva ut noll- och icke-nolltal för godkända tecken i C

Låt oss skriva ett program för att kontrollera alla givna tecken och returnerar noll- och icke-nollvärden baserat på de skickade tecknen till isdigit()-funktionen i C.

hur man skriver ut 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; } 

Produktion:

 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 

I programmet ovan testar vi för giltiga givna siffror med hjälp av isdigit()-funktionen. Funktionen isdigit() returnerar 1 till de numeriska tecknen och 0 till de alfabetiska eller specialsymboler som definieras i programmet som inte är siffror.

Exempel 4: Program för att kontrollera alla arrayelement med hjälp av isdigit()-funktionen

Låt oss skriva ett program för att hitta alla giltiga siffror i arrayelementet med funktionen isdigit() i 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;>

Vi deklarerade och initierade variabeln arr[] med några element i programmet ovan. Och sedan skapar vi en annan array arr2[] som innehåller noll (0) för att tilldela resultatet till de element som inte är giltiga siffror. Funktionen isdigit() kontrollerar alla arr[]-matriselement och returnerar värden som inte är noll till de giltiga sifferelementen. Annars returnerar den nollor (0) till icke-siffriga element.