• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

[2 Methods] C Program to Implement atoi() Function | Convert String to Int

Aniruddha Chaudhari/16221/0
C / C++Code

The string is nothing but a group of characters.

String Example: "abcd"

These characters can also be the numbers.

String Example (consist of numebrs): "1234"

If you add the double quotation to the number, it becomes the string. Even it contains the number values, you can not perform any mathematical operation. The compiler considers it as a pure string, not a number.

Many times in your project you need to convert this string to the number so that you can perform mathematical calculations.

You can convert the string to an integer by manually or you can also use the inbuilt function atoi(). Before going to the inbuilt function, we do write our own code.

Or in interviews, you may be asked-  Write a C Program to implement atoi() function.

Converting String to Int in C code

1. C Program to Implement atoi() Function:

We read each character from the string and based on the digit position on the character in the string, we will multiply by a power of ten.

Let’s take an example. We want to convert the string “354” into integer value as 354.

Read 3rd character:
4 * 10^0 = 4

Read 2nd character
5*10^1 = 50

Read 1st character
3*10^2 = 300

Calculate the sum of all numbers
4+50+300 = 354

We read the characters from the string in reverse order. To read the last character first, you need to find out the number of characters in the string for which you can use function strlen().

Prerequisite:

strlen() function in C – It returns the number of characters in the given string.

ASCII value of the integer character. You can refer to the ASCII value in the table below.

Character ASCII value
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57

Program to write to your own atoi() function:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char strNum[100] = {0};
    int nVal = 0, i, j, len;
    printf("Enter a number string: ");
    scanf("%s",&strNum);
    printf("\nYour string is %s", strNum);
 
    len = strlen(strNum);
    int nDecCount = 1;
 
    for(int i=len-1; i>=0; i--){
        nVal = nVal + nDecCount * ( strNum[i] - '0' );
        nDecCount *=10;
    }
    printf("\nYour integer value is %d", nVal);
    return 0;
}

Ouput of the Program:

Enter a number string: 1453
Your string is 1453.
Your integer value is 1453

You can perform any mathematical operation on this result integer value.

2. Convert String to Int Using Built-in Function atoi()

If you don’t want to do this all cumbersome operations on the string, you can simply use in build function atoi() to convert the string into an integer value.

Syntax of atoi():

int atoi(const char *str)

It takes the string as an input value and gives corresponding integer value as an output.

The functionatoi() is available in stdlib.h header file. So, you need to include in your program.

Program using atoi():

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char strNum[100] = {0};
    int nVal = 0;
    printf("Enter a number: ");
    scanf("%s",&strNum);
 
    nVal = atoi(strNum);
    printf("%d", nVal);
    return 0;
}

If you are working on any project, you are free to use an inbuilt function rather than doing it manually. But many times, the interviewer asks you to implement a manual function to test your logic building skill.

Other String related C Program you can try:

  • Code to Implement strstr Function in C
  • Check if String is Palindrome or Not
  • Check if Two Strings are Anagram or Not

This is all about the C Program to Implement atoi() function. There can be multiple methods to do the same. If you have any other methods, write in the comment section.

atoistring
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard