A string is the data type used to hold any content in the C programming language, containing alphabetic and numeric characters. In C, a string is ended with a NULL character; hence this is shown by the term “null-terminated strings.” Encapsulate a string in double quotations to express this in C. Fundamentally; a string is shown in C, where 0 signifies the null character.
Strings and their attributes have been used in a majority of C programs. The is a required library for string methods. Determining the length of a string, concatenating several strings, associating various strings, and modifying strings are all processes that can be performed on strings.
In this tutorial, we’ll look at how to transform strings into integers (numerical values) while using the C programming language.
Transform a String to an Integer with the help of the atoi() method:
In the C programming language, the atoi() method transforms a string into an integer. At the start of the string, the atoi() method ignores all-white spaces, translates the words just after white spaces, and then ceases when it hits the first non-number word. The atoi() module implements the string’s integer depiction.
#include <stdlib.h>
#include <string.h>
int main (void)
{
int val;
char string[30];
strcpy(string,"456");
val = atoi(string);
printf("The string value is %s \n The integer value is %d\n", string, val);
return(0);
}
At the start of the program, we integrate three header files: <stdio.h> <stdlib.h> and <string.h>. These header files enable the use of different functions. With the help of <stdlib.h> header file, we apply the atoi() method. In the next step, we start the main function of the code. Further, we initialize the variable having an integer data type.
Similarly, we set the size of the string array, and the string is of character data type. Then we apply the strcpy() function to declare the string. The atoi() function is called to convert the defined string into an integer. Further, we employ the printf() function to print integer and string values. This is how we end the main function.
After running the code, we obtain the string and integer values of the defined value by using the atoi() function.
Use the stringstream class to transform a String into an Integer:
In current versions of the C programming language, the stringstream class has also been used. It operates by using strings to execute inputs and outputs. The stringclass is also used to transform the value of a string data type into an integer data type. The following instances show us the use of stringclass.
#include <string>
#include <sstream>
using namespace std;
int main() {
stringstream ss;
string st = "893449";
int n;
ss <> n;
printf("%d", n);
return 0;
}
First of all, we include libraries. <stdio.h> library is used for input and output functionality. The header file <sstream> is included to handle stringstream in the code. Now we use the standard namespace function. In the body of the main() function, we declare class stringstream and then construct its object to input and output strings. Further, we initialize a variable having a string data type. This variable is used to store the value of the string, which we want to modify into the integer data type.
In the next line, we state another variable, ‘n’, which has an integer data type. Meanwhile, we excerpt the defined string from the string ‘st’ variable. For the extraction, we utilize the <> operator is used to insert the newly modified integer value. Finally, to get the integer value, we apply the printf() function.
Use the strtol() method to translate a String to an Integer:
In the C programming language, the strtol() method transforms a value having a string data type into a long integer. The strtol() method skips all white-space characters at the start of the string, translates the consecutive characters as an element of the proportion, and afterward terminates when it reaches the first non-number character. The strtol() method computes a string’s long integer illustration.
Here’s an instance of how to translate a string into an integer with the help of the strtol() function.
#include <stdlib.h>
#include <string.h>
int main(void)
{
char st[30];
char *ptr;
long val;
strcpy(st, "045086");
val = strtol(st, &ptr, 10);
printf("The decimal value : %ld\n", val);
return 0;
}
Here the first step is to introduce the required libraries <stdio.h>, <stdlib> and <string.h>. We declare main() function. In the body of the main function, we create an array of strings, and here we specify the size of this array. Now we construct a pointer having a character data type. In the same way, we initialized a variable for a long value.
Further, we employ strcpy() and strtol() methods. The strtol() function contains three parameters. First, it holds the value of the string which we want to be converted into an integer. The second parameter is a pointer, which specifies where the transformation ends. The last parameter shows the range of the base. The printf() method is called to print the result. This is how we terminate the program.
By executing the above-mentioned program, we get the ‘The decimal value:’ of the given string ‘045086’.
Conclusion:
In this article, we talked about different methods to convert the defined string to an integer. We have seen the atoi() function, stringstream class, and strtol() function for converting the value of string data type to integer data type. Three different examples have been implemented and explained to clear the concept.