5

C Program to Convert String to Integer

 2 years ago
source link: https://www.geeksforgeeks.org/videos/c-program-to-convert-string-to-integer/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C Program to Convert String to Integer

C Program to Convert String to Integer
  • 730 Views
  • 30/07/2022

In this video, we will see a C program to convert strings to integers. 

What are Strings?
Strings are defined as an array of characters. 

Here we will see two different methods for converting a string to an integer:

1. Using Strlen and for loop
2. Using In-built function atoi()

1) Using Strlen and for loop:
Step 1: Frist we store the length of the string.
Step 2: Iterating the for loop.
Num = Num * 10 + (s[i]-'0')
Step 3: Print the result.

2) Using In-built function atoi():
The atoi()function is defined inside stdlib.h header file. It converts the string parameter to an integer. If conversion doesn't exists for that string then it returns zero.
Syntax: int atoi(const char *str);

Apart from that, we will see the time and space complexity of both methods.

Convert string to integer:
https://www.geeksforgeeks.org/convert-string-to-integer-without-using-any-in-built-functions/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK