Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. 2-2
  2. ==========
  3. #include <stdio.h>
  4.  
  5. #define MAX_STRING_LENGTH 100
  6.  
  7. int main(void)
  8. {
  9. /*
  10. for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
  11. s[i] = c;
  12. */
  13.  
  14. int i = 0,
  15. lim = MAX_STRING_LENGTH,
  16. c;
  17. char s[MAX_STRING_LENGTH];
  18.  
  19. while (i < (lim - 1))
  20. {
  21. c = getchar();
  22.  
  23. if (c == EOF)
  24. break;
  25. else if (c == '\n')
  26. break;
  27.  
  28. s[i++] = c;
  29. }
  30.  
  31. s[i] = '\0'; /* terminate the string */
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37. 2-3
  38. ==========
  39. #include <stdio.h>
  40. #include <math.h>
  41.  
  42. int upperToLower(int character);
  43.  
  44. int main(void){
  45. char hexString[10];
  46. int i, j, c, input, integer;
  47.  
  48. char hexadecimals[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
  49. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  50.  
  51. i = 0;
  52. while ((c = getchar()) != EOF)
  53. {
  54. if (c >= 'A' & c <= 'Z')
  55. {
  56. c = upperToLower(c);
  57. }
  58. hexString[i] = c;
  59. ++i;
  60. }
  61. hexString[i] = '\0';
  62.  
  63. integer = 0;
  64. for (j = i - 1; j >= 0; --j)
  65. {
  66. if(hexString[j] >= 'a' && hexString[j] <= 'z')
  67. {
  68.  
  69. for(int k = 0; k < 16; ++k)
  70. {
  71. if(hexString[j] == hexadecimals[k])
  72. {
  73. integer = integer + k * pow(16, i-1-j);
  74. }
  75. }
  76. }
  77. else
  78. integer = integer + (hexString[j] - '0') * pow(16, i-1-j);
  79. }
  80.  
  81. printf("\nThe integer value of the hexadecimal string %s is ", hexString);
  82. printf("d.\n", integer);
  83. }
  84.  
  85. int upperToLower(int input){
  86. int lower;
  87.  
  88. lower = input + 'a' - 'A';
  89.  
  90. return lower;
  91. }
  92.  
  93.  
  94. 2-4
  95. ==========
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. 2-5
  106. ==========
  107. int any(char s1[], char s2[])
  108. {
  109. int i;
  110. int j;
  111. int pos;
  112.  
  113. pos = -1;
  114.  
  115. for(i = 0; pos == -1 && s1[i] != '\0'; i++)
  116. {
  117. for(j = 0; pos == -1 && s2[j] != '\0'; j++)
  118. {
  119. if(s2[j] == s1[i])
  120. {
  121. pos = i;
  122. }
  123. }
  124. }
  125.  
  126. return pos;
  127. }
  128.  
  129.  
  130. 2-6
  131. ==========
  132.  
  133.  
  134. 2-7
  135. ==========
  136.  
  137. 2-8
  138. ==========
  139.  
  140. 2-9
  141. ==========
  142. int bitcount(unsigned x) {
  143. int b=0;
  144. while (x != 0) {
  145. x &= (x-1);
  146. b++;
  147. }
  148.  
  149. return b;
  150. }
  151.  
  152. int main() {
  153. printf("%d\n", bitcount(25));
  154. }
  155.  
  156.  
  157. 2-10
  158. ==========
  159. #include <stdio.h>
  160.  
  161. int lower(int c) {
  162. return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
  163. }
  164.  
  165.  
  166. 3-1
  167. ==========
  168. int binsearch2(int x, int v[], int n)
  169. {
  170. int low, high, mid;
  171.  
  172. low = -1;
  173. high = n;
  174. while (low + 1 < high) {
  175. mid = (low + high) / 2;
  176. if (v[mid] < x)
  177. low = mid;
  178. else
  179. high = mid;
  180. }
  181. if (high == n || v[high] != x)
  182. return -1;
  183. else
  184. return high;
  185. }
  186.  
  187.  
  188.  
  189. 3-2
  190. ==========
  191. void escape(char * s, char * t) {
  192. int i, j;
  193. i = j = 0;
  194.  
  195. while ( t[i] ) {
  196.  
  197. /* Translate the special character, if we have one */
  198.  
  199. switch( t[i] ) {
  200. case '\n':
  201. s[j++] = '\\';
  202. s[j] = 'n';
  203. break;
  204.  
  205. case '\t':
  206. s[j++] = '\\';
  207. s[j] = 't';
  208. break;
  209.  
  210. case '\a':
  211. s[j++] = '\\';
  212. s[j] = 'a';
  213. break;
  214.  
  215. case '\b':
  216. s[j++] = '\\';
  217. s[j] = 'b';
  218. break;
  219.  
  220. case '\f':
  221. s[j++] = '\\';
  222. s[j] = 'f';
  223. break;
  224.  
  225. case '\r':
  226. s[j++] = '\\';
  227. s[j] = 'r';
  228. break;
  229.  
  230. case '\v':
  231. s[j++] = '\\';
  232. s[j] = 'v';
  233. break;
  234.  
  235. case '\\':
  236. s[j++] = '\\';
  237. s[j] = '\\';
  238. break;
  239.  
  240. case '\"':
  241. s[j++] = '\\';
  242. s[j] = '\"';
  243. break;
  244.  
  245. default:
  246.  
  247. /* This is not a special character, so just copy it */
  248.  
  249. s[j] = t[i];
  250. break;
  251. }
  252. ++i;
  253. ++j;
  254. }
  255. s[j] = t[i]; /* Don't forget the null character */
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement