
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Converting string to number and vice-versa in C++
In this section we will see how to convert string to number and number to string. At first we will see how to convert string to number.
String to Number Conversion
Here we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.
The atoi() function is present in the <cstdlib> library.
Input: A number string “1234” Output: 1234
Algorithm
Step 1:Take a number string Step 2: Convert it to integer using atoi() function Step 3: Print the result. Step 4: End
Example Code
#include<iostream> #include<cstdlib> using namespace std; main() { int n; char num_string[20] = "1234"; n = atoi(num_string); cout << n; }
Output
1234
Number to String Conversion
In this section we will see how to convert a number (integer or float or any other numeric type data) to a string.
The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.
Input: User will put some numeric value say 42.26 Output: This program will return the string equivalent result of that number like “42.26”
Algorithm
Step 1: Take a number from the user Step 2: Create an empty string buffer to store result Step 3: Use sprintf() to convert number to string Step 4: End
Example Code
#include<stdio.h> main() { char str[20]; //create an empty string to store number float number; printf("Enter a number: "); scanf("%f", &number); sprintf(str, "%f", number);//make the number into string using sprintf function printf("You have entered: %s", str); }
Output
Enter a number: 46.3258 You have entered: 46.325802
- Related Articles
- Converting Strings to Numbers and Vice Versa in Java.
- Convert string to DateTime and vice-versa in Python
- Python – Test String in Character List and vice-versa
- How to convert String to StringBuilder and vice versa Java?
- Golang program to convert the arraylist into string and vice-versa
- C++ Program to Convert Binary Number to Octal and vice-versa
- C++ Program to Convert Binary Number to Decimal and vice-versa
- C++ Program to convert Octal Number to Decimal and vice-versa
- How to Convert a String to Hexadecimal and vice versa format in java?
- Binary to decimal and vice-versa in Python
- Java Program to Convert the ArrayList into a string and vice versa
- How to Convert Letter to Number or Vice Versa in Excel?
- Adding Tuple to List and vice versa in Python
- How to Convert Week Number to Date or Vice Versa in Excel?
- C program to convert upper case to lower and vice versa by using string concepts
