Advertisement
avr39ripe

cppNumToStr_StrToNum

May 25th, 2021
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. char* numToStr(int num, char* str)
  4. {
  5.     char tmp{};
  6.  
  7.     for (size_t i{ 0 }; *(str + i) = (num > 0 ? '0' + (num % 10) : 0); num /= 10, ++i);
  8.  
  9.     for (size_t h{ 0 }, t{ strlen(str) - 1 }; h < t; ++h, --t)
  10.     {
  11.         tmp = *(str + h);
  12.         *(str + h) = *(str + t);
  13.         *(str + t) = tmp;
  14.     }
  15.     return str;
  16. }
  17.  
  18. int strToNum(const char* str)
  19. {
  20.     int num{ 0 };
  21.     for (; *str; num *= 10, num += *str++ - '0');
  22.  
  23.     return num;
  24. }
  25.  
  26. int main()
  27. {
  28.     char str[10];
  29.     std::cout << numToStr(26907, str) << '\n';
  30.     std::cout << strToNum(str) << '\n';
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement