Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. int atoi(char* string)
  2. {
  3. int pos = 0;
  4. int result = 0;
  5. int factor = 1;
  6. char letter;
  7.  
  8. if (*string == '-')
  9. {
  10. factor = -1;
  11. string++;
  12. }
  13.  
  14. while (*(string+pos)!='\0') pos++;
  15. while (pos>0)
  16. {
  17. letter = *(string+pos-1);
  18. if (letter < '0' || letter > '9')
  19. return 0;
  20. result += (letter - '0') * factor;
  21. factor *= 10;
  22. pos --;
  23. }
  24. return result;
  25. }
  26.  
  27. char* strcat(char* str1, char* str2)
  28. {
  29. int lenstr1 = strlen(str1);
  30. int lenstr2 = strlen(str2);
  31. char* result = (char*)malloc(lenstr1+lenstr2);
  32. char* pos = result;
  33.  
  34. while(lenstr1>0)
  35. {
  36. *pos++ = *str1++;
  37. lenstr1--;
  38. }
  39. while(lenstr2>0)
  40. {
  41. *pos++ = *str2++;
  42. lenstr2--;
  43. }
  44.  
  45. return result;
  46. }
  47.  
  48. char* basename(char* path)
  49. {
  50. char* p = path+strlen(path)-1;
  51. while (p > path)
  52. {
  53. if(*p == '/')
  54. return p+1;
  55. p--;
  56. }
  57. return path;
  58.  
  59. }
  60.  
  61. int countones(int number)
  62. {
  63. int count = 0;
  64. int pattern = 1;
  65. while (pattern <= number)
  66. {
  67. if ((number & pattern) > 0)
  68. {
  69. count++;
  70. }
  71. pattern *= 2;
  72. }
  73. return count;
  74. }
  75.  
  76. unsigned int rightrotate(unsigned int number, int count)
  77. {
  78. unsigned int leftover = 0;
  79. while(count-- > 0)
  80. {
  81. leftover = number << (sizeof(unsigned int)*8-1);
  82. number = (number >> 1) | leftover;
  83. }
  84. return number;
  85. }
Add Comment
Please, Sign In to add comment