Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int GetLength(char* str) // ['m','a','r','i','a','m','i','\0']
  6. {
  7. int i = 0;
  8. int counter = 0;
  9. while (str[i] != '\0')
  10. {
  11. counter++;
  12. i++;
  13. }
  14.  
  15. return counter;
  16. }
  17.  
  18. int stringToInt(char* str)
  19. {
  20. int length = GetLength(str);
  21. int result = 0;
  22.  
  23.  
  24. //540
  25. for (int i = 0; i < length; i++)
  26. {
  27. result = result * 10 + (str[i] - '0'); // '0' = 48
  28. }
  29.  
  30. return result;
  31. }
  32.  
  33.  
  34.  
  35.  
  36. int main()
  37. {
  38.  
  39.  
  40. char Array[] = {"14955"};
  41.  
  42.  
  43. int res = stringToInt(Array);
  44.  
  45. cout << res;
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. cin.get();
  53. cin.get();
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement