how to make string (-1) into integer -1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to make string (-1) into integer -1?

if i input string 100(-1)0, i want to make this string become 5 integer 1,0,0,-1,0

3rd Nov 2018, 1:16 PM
Yoaraci
Yoaraci - avatar
17 Answers
+ 1
Yoaraci // here is a code in c++: #include <iostream> #include <vector> #include <string> using namespace std; int main() { string userinput; vector <int> num; getline(cin, userinput); for (int index = 0; index<userinput.size(); index++) { if (index != 0 && userinput[index - 1] == '-') { if (isdigit(userinput[index])) { int N = '0' - userinput[index]; num.push_back(N); } } else { if (isdigit(userinput[index])) { int N = userinput[index] - '0'; num.push_back(N); } } } for (int i = 0; i<num.size(); i++) { cout << num[i] << endl; } system("pause"); return 0; }
3rd Nov 2018, 2:48 PM
Suraj Jha
Suraj Jha - avatar
+ 4
A way which works with both C and C++ : atoi // C++ // include string and cstdlib int a = atoi(s.c_str()); //with s of type std::string // C // include stdlib.h int a = atoi(s); // with s an array of chars terminating by a null character A C++ like solution which works with all types that overload the >> operator (int does) : // include sstream std::istringstream iss(s); // with s your string int a; iss >> a;
4th Nov 2018, 9:30 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Baptiste E. Prunier Will the stringstream solution work for the sample string as mentioned in original post? "100(-1)0", somehow I doubt it will, how does the stringstream object decide what to do with the parentheses?
4th Nov 2018, 10:13 AM
Ipang
3rd Nov 2018, 2:57 PM
Suraj Jha
Suraj Jha - avatar
+ 2
vector is same as array in c++ only difference is we do not need to specify it's size; .push_back() is used to add an element at the end of a vector ( i.e array)
3rd Nov 2018, 3:00 PM
Suraj Jha
Suraj Jha - avatar
+ 2
Yoaraci, what I can see you haven't completed even half of course in c++; It may be difficult to understand; In case you have any doubt please don't hesitate to ask; 😊😊😊
3rd Nov 2018, 3:05 PM
Suraj Jha
Suraj Jha - avatar
+ 2
Ipang you will have to change it a bit, it will give you 100, -1 and 0 (3 integers), you will have to split 100 yourself
4th Nov 2018, 12:09 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
If are you looking for javascript code: function start() { var userinput = document.getElementById("userInput").value; let sign = ""; let num=[]; for (var index=0; index<userinput.length; index++) { if (index !== 0 && userinput[index-1] === '+') { sign="+"; } else if (index !== 0 && userinput[index-1] === '-') { sign = '-'; } if (!isNaN(userinput[index])) { num.push(parseInt(sign+userinput[index])); sign=""; } } document.getElementById("output").innerHTML = num; } same logic​ can be used in different programing language!!
3rd Nov 2018, 2:01 PM
Suraj Jha
Suraj Jha - avatar
+ 1
Yoaraci, did you understood the logic or I should give an example in c++ ?
3rd Nov 2018, 2:15 PM
Suraj Jha
Suraj Jha - avatar
+ 1
Yoaraci , did you get this!!!! or say I will explain!!
3rd Nov 2018, 2:53 PM
Suraj Jha
Suraj Jha - avatar
+ 1
Yoaraci did you really understand??
3rd Nov 2018, 3:00 PM
Suraj Jha
Suraj Jha - avatar
0
actually i never learn java so i dont really understand the logic😅... so, yes i hope you can give me the example in c++ 😁
3rd Nov 2018, 2:18 PM
Yoaraci
Yoaraci - avatar
0
can i ask what the function from vector<int>num and num.push_back?
3rd Nov 2018, 2:56 PM
Yoaraci
Yoaraci - avatar
0
okay thanks 😁
3rd Nov 2018, 2:59 PM
Yoaraci
Yoaraci - avatar
0
thanks hehehe... i actually learn only c in my school and i also new in coding hahaha.. but i understand a little about c++... i will ask you again if there are something that i dont understand thank youu😁😁😁
3rd Nov 2018, 3:32 PM
Yoaraci
Yoaraci - avatar
- 1
oh sorry i forgot to add it, i need c or c++ but i more prefer with c... but thanks for answering :)
3rd Nov 2018, 2:07 PM
Yoaraci
Yoaraci - avatar
- 1
thanksss 😀😀😀
3rd Nov 2018, 2:53 PM
Yoaraci
Yoaraci - avatar