इस ट्यूटोरियल में, हम यह समझने के लिए एक प्रोग्राम पर चर्चा करेंगे कि स्ट्रिंग्स को C/C++ में संख्याओं में कैसे परिवर्तित किया जाए।
C/C++ स्ट्रिंग्स को संख्याओं में बदलने के दो तरीके प्रदान करता है।
उदाहरण
sscanf() का उपयोग करना
#include<stdio.h>
int main(){
const char *str = "12345";
int x;
sscanf(str, "%d", &x);
printf("\nThe value of x : %d", x);
return 0;
} आउटपुट
The value of x : 12345
स्टोई का उपयोग करना ()
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1 = "45";
string str2 = "3.14159";
string str3 = "31337 geek";
int myint1 = stoi(str1);
int myint2 = stoi(str2);
int myint3 = stoi(str3);
cout << "stoi(\"" << str1 << "\") is " << myint1 << '\n';
cout << "stoi(\"" << str2 << "\") is "<< myint2 << '\n';
cout << "stoi(\"" << str3 << "\") is "<< myint3 << '\n';
return 0;
} आउटपुट
stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 geek") is 31337