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


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
", string1);    printf("Integer value = %d
", value);    char string2[] = "TutorialsPoint";    value = atoi(string2);    printf("String value = %s
", string2);    printf("Integer value = %d
", value);    return (0); }

Output

String value = 3567
Integer value = 3567
String value = TutorialsPoint
Integer value = 0

Updated on: 09-Mar-2021

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements