Advertisement
CSenshi

Int <-> String (Old)

Mar 27th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. //converter from int to string
  2. string intToString(int n){
  3.     if(n>=0){
  4.         if(n<10){
  5.             return string (string() + char(n+'0'));
  6.         }else{
  7.             return string(intToString(n/10) + char(n%10+'0'));
  8.         }
  9.     }else{
  10.         return "-"+intToString(-n);
  11.     }
  12. }
  13.  
  14. //converter from string to int
  15. int stringToInt(string str){
  16.     if(str[0]!='-'){
  17.         if(str.length()==1){
  18.             return int(str[0]-'0');
  19.         }else{
  20.             return stringToInt(str.substr(0,str.length()-1))*10 + int(str[str.length()-1]-'0');
  21.         }
  22.     }else{
  23.         return 0-stringToInt(str.substr(1,str.length()-1));
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement