Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. (buffer[0] != '')
  2.  
  3. (buffer[0] != 'n')
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void){
  8.  
  9. int count = 0;
  10. char buffer[80];
  11.  
  12. char *myDynamicArray[25];
  13. int i = 0;
  14. int k = 0;
  15.  
  16. puts("Enter integers, press space to end");
  17.  
  18. while ((i < 25) && (gets(buffer) != 0) && (buffer[0] != ''))
  19. {
  20. if ((myDynamicArray[i] = (char *)malloc(strlen(buffer) + 1)) == NULL)
  21. return -1;
  22. strcpy(myDynamicArray[i++], buffer);
  23.  
  24. }
  25.  
  26. count = i;
  27.  
  28. for (k = 0; k < count; k++)
  29. {
  30. printf("%sn", myDynamicArray[k]);
  31. }
  32.  
  33. getchar();
  34.  
  35. }
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40.  
  41. int main ( void )
  42. {
  43. int c, i, j=0;
  44. char buffer[80], *dynArray[5];
  45. for (i=0;i<5;++i)
  46. {
  47. j = 0;
  48. while((c = getchar()) != EOF && c != 'n' && j < 79)
  49. buffer[j++] = c;
  50. if (j == 0)
  51. {//an empty string was encountered!
  52. for (--i;i>-1;--i)
  53. {//use --i, because current i is not allocated yet!
  54. printf("Freeing memory containing "%s"n", dynArray[i]);
  55. free(dynArray[i]);
  56. }
  57. return EXIT_SUCCESS;//exit program here, then
  58. }
  59. buffer[j] = '';//add NULL-char
  60. dynArray[i] = calloc(j, sizeof *dynArray[i]);//calloc to initialize memory
  61. if (dynArray[i] == NULL)
  62. exit(EXIT_FAILURE);
  63. strcpy(dynArray[i], buffer);
  64. }
  65. //allow user to see a specific input string again:
  66. puts("Choose a string to see again [1-5], or enter q to proceed");
  67. while ((c = getchar()) != EOF && c != 'q')
  68. {
  69. c -= (1 + '0');//simple atoi trick
  70. if (c < 5 && c > -1)//input char is 1, 2, 3, 4 or 5
  71. printf("string %d: %sn", c+1, dynArray[c]);
  72. }
  73. //q was fetched from stdin, clear buffer...
  74. while((c = getchar()) != EOF && c != 'n');
  75. for (i=0;i<5;++i)
  76. {
  77. printf("free-ing memory containing: %sn", dynArray[i]);
  78. free(dynArray[i]);
  79. }
  80. getchar();//press any key to continue...
  81. return 0;
  82. }
  83.  
  84. #include <stdio.h>
  85. #include <stdlib.h>
  86. #include <string.h>
  87.  
  88. int get_key( const int max )
  89. {
  90. int c, r = 0;
  91. while ((c = getchar()) != EOF && c != 'q' && c != 'n')
  92. {
  93. c -= '0';
  94. if (c > -1 && c < 10)
  95. {
  96. if (r > 0)//suppose input was 22, r will be 2 on second iteration of loop
  97. r *=10;//multiply by 10, to get 20
  98. r += c;//add 2 == 22
  99. }//optionally add else, clean buffer and procede if r > 0
  100. }
  101. if (r > max || r == 0)//perhaps move to loop body
  102. r = -1;//out-of-bounds request = -2
  103. if (c == 'q')
  104. {
  105. r = 0;//stop = -1
  106. //clean buffer
  107. while (c != EOF && c != 'n')
  108. c = getchar();
  109. }
  110. return r-1;//subtract 1 to get array index (zero indexed)
  111. }
  112.  
  113. int main ( void )
  114. {
  115. int c, k, i;
  116. char buffer[80], *myDynamicArray[25];
  117. puts("Enter string");
  118. for (k=0;k<25;++k)
  119. {
  120. i=0;
  121. while((c = getchar()) != EOF && c != 'n' && i < 79)
  122. buffer[i++] = c;
  123. if (i == 0)
  124. break;
  125. if (i == 79)
  126. while (c != EOF && c != 'n')
  127. c = getchar();//clean buffer
  128. buffer[i] = '';
  129. myDynamicArray[k] = calloc(i, sizeof *myDynamicArray[k]);
  130. if (myDynamicArray[k] == NULL)
  131. exit(EXIT_FAILURE);
  132. strcpy(myDynamicArray[k], buffer);
  133. }
  134. printf("Choose which string you'd like to see again [1-%d], press q to quitn", k);
  135. while ((c = get_key(k)) != -1)
  136. if (c != -2)
  137. printf("String %d: %sn", c+1, myDynamicArray[c]);
  138. else
  139. printf("input must be in the 1 - %d rangen", k);
  140. for (i=0;i<k;++i)
  141. {
  142. printf("%sn", myDynamicArray[i]);
  143. free(myDynamicArray[i]);
  144. }
  145. printf("Press any key to continue...");
  146. getchar();
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement