How to create a customized atoi() function in C language?

CServer Side ProgrammingProgramming

The atoi() is predefined function used to convert a numeric string to its integer value.

Create a customized atoi()

The atoi() only converts a numeric string to integer value, so we need to check the validity of the string.

If this function encounters any non-numeric character in the given string, the conversion from string to integer will be stopped.

Example

 Live Demo

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
   int value;
   char string1[] = "3567";
   value = atoi(string1);
   printf("String value = %s\n", string1);
   printf("Integer value = %d\n", value);
   char string2[] = "TutorialsPoint";
   value = atoi(string2);
   printf("String value = %s\n", string2);
   printf("Integer value = %d\n", value);
   return (0);
}

Output

String value = 3567
Integer value = 3567
String value = TutorialsPoint
Integer value = 0
raja
Updated on 09-Mar-2021 09:31:03

Advertisements