Guest User

Untitled

a guest
Aug 17th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. Sorting strings doesn't work properly
  2. mate sime, jure
  3. stipica gujo, prvi
  4. ante mirkec
  5. goran maja, majica
  6. avion kuca, brod, seoce
  7. amerika, neka, zemlja, krcma
  8. brodarica, zgrada, zagreb
  9. zagreb split
  10. zadar rijeka
  11. andaluzija azija
  12.  
  13. andaluzija azijamate sime, jure
  14. amerika, neka, zemlja, krcma
  15. brodarica, zgrada, zagreb
  16. ante mirkec
  17. avion kuca, brod, seoce
  18. goran maja, majica
  19. stipica gujo, prvi
  20. zadar rijeka
  21. zagreb split
  22.  
  23. andaluzija azijamate sime, jure
  24. amerika, neka, zemlja, krŔma
  25. brodarica, zgrada, zagreb
  26. ante mirkec
  27. avion kuŠa, brod, seoce
  28. goran maja, majica
  29. stipica gujo, prvi
  30. zadar rijeka
  31. zagreb split
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. int main()
  37. {
  38. int ch, nl = 1, min, lenght1, lenght2, lenght;//ch will hold characters, min is for selection sort, lenght holds value of strlen for determine wthat line is longer
  39. FILE * fp;// FILE pointer
  40. char * lines[1000];//that will dynamically hold strings for lines
  41. char * temp;//for lines swaping
  42. if((fp = fopen("C:\Users\don\Documents\NetBeansProjects\proba2\dist\Debug\MinGW-Windows\rjecnik.txt", "r")) == NULL)//I had to temporarily put full path to rjecnik.txt
  43. {
  44. printf("Can't open file...");
  45. exit(1);
  46. }
  47. while((ch = getc(fp)) != EOF)//count lines
  48. {
  49. if(ch == 'n')
  50. nl++;
  51. }
  52. int i, j;
  53. for (i = 0; i < nl; i++)
  54. lines[i] = malloc(1000);//create array of string size value of nl
  55. fseek(fp, 0L, SEEK_SET);//go to start of file
  56. i = 0;
  57. j = 0;
  58. while((ch = getc(fp)) != EOF)//fill arrays of string
  59. {
  60. lines[i][j] = ch;
  61. j++;
  62. if(ch == 'n')
  63. {
  64. j = 0;
  65. i++;
  66. }
  67. }
  68. for(i = 0; i < nl - 1; i++)//selection sort doesn't work properly
  69. {
  70. min = i;//min is i
  71. for(j = i + 1; j < nl; j++)//for number of lines(nl) times
  72. {
  73. lenght1 = strlen(lines[i]);//find what string is longer and lenght is smaller one
  74. lenght2 = strlen(lines[j]);
  75. if(lenght1 < lenght2)
  76. lenght = lenght1;
  77. else
  78. lenght = lenght2;
  79. if(strncmp(lines[i], lines[j], lenght) > 0 )//compare two strings
  80. min = j;//if second string is alphabetically smaller min is j
  81. }
  82. temp = lines[i];// swapping
  83. lines[i] = lines[min];
  84. lines[min] = temp;
  85. }
  86. for(i = 0; i < nl; i++ )//printing to console
  87. {
  88. lenght1 = strlen(lines[i]);
  89. for(j = 0; j < lenght1; j++ )
  90. {
  91. putchar(lines[i][j]);
  92. }
  93. }
  94. return 0;
  95. }
  96.  
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100. int main()
  101. {
  102. int ch, nl = 1, min, lenght1, lenght2, lenght;//ch will hold characters, min is for selection sort, lenght holds value of strlen for determine wthat line is longer
  103. FILE * fp;// FILE pointer
  104. char * lines[1000];//that will dynamically hold strings for lines
  105. char * temp;//for lines swaping
  106. if((fp = fopen("C:\Users\don\Documents\NetBeansProjects\proba2\dist\Debug\MinGW-Windows\rjecnik.txt", "r")) == NULL)//I had to temporarily put full path to rjecnik.txt
  107. {
  108. printf("Can't open file...");
  109. exit(1);
  110. }
  111. while((ch = getc(fp)) != EOF)//count lines
  112. {
  113. if(ch == 'n')
  114. nl++;
  115. }
  116. int i, j;
  117. for (i = 0; i < nl; i++)
  118. lines[i] = malloc(1000);//create array of string size value of nl
  119. fseek(fp, 0L, SEEK_SET);//go to start of file
  120. i = 0;
  121. j = 0;
  122. while((ch = getc(fp)) != EOF)//fill arrays of string
  123. {
  124. lines[i][j] = ch;
  125. j++;
  126. if(ch == 'n')
  127. {
  128. j = 0;
  129. i++;
  130. }
  131. }
  132. for(i = 0; i < nl - 1; i++)//selection sort doesn't work properly
  133. {
  134. min = i;//min is i
  135. for(j = i + 1; j < nl; j++)//for number of lines(nl) times
  136. {
  137. lenght1 = strlen(lines[i]);//find what string is longer and lenght is smaller one
  138. lenght2 = strlen(lines[j]);
  139. if(lenght1 < lenght2)
  140. lenght = lenght1;
  141. else
  142. lenght = lenght2;
  143. if(strncmp(lines[min], lines[j], lenght ) > 0 )//compare two strings
  144. min = j;//if second string is alphabetically smaller min is j
  145. }
  146. temp = lines[i];// swapping
  147. lines[i] = lines[min];
  148. lines[min] = temp;
  149. }
  150. for(i = 0; i < nl; i++ )//printing to console
  151. {
  152. lenght1 = strlen(lines[i]);
  153. for(j = 0; j < lenght1; j++ )
  154. {
  155. putchar(lines[i][j]);
  156. }
  157. }
  158. for (i = 0; i < 100; i++)//Program crashes here
  159. free(lines[i]);
  160.  
  161. return 0;
  162. }
  163.  
  164. int strsort(const void *a, const void *b)
  165. {
  166. char *const*astr=a, *const*bstr=b;
  167. return strcmp(*astr, *bstr);
  168. }
  169.  
  170. main()
  171. {
  172. FILE*f = fopen(...);
  173. char (*arr)[1000] = malloc(1000*1000);
  174. int x;
  175. for(x=0;x<1000 && fgets(1000, arr[x], f);x++)
  176. arr[x][strlen(arr[x])-2] = ''; //strip newlines
  177. qsort(arr, x, 1, strsort);
  178. int i;
  179. for(i=0; i<x; i++)
  180. printf("%sn", arr[x]);
  181. }
  182.  
  183. lenght1 = strlen(lines[i]);
  184. lenght2 = strlen(lines[j]);
  185. if(lenght1 < lenght2)
  186. lenght = lenght1;
  187. else
  188. lenght = lenght2;
  189. if(strncmp(lines[i], lines[j], lenght) > 0 )
  190. ... ;
  191.  
  192. strncmp( lines[i], lines[j], lenght+1)
  193.  
  194. strcmp(lines[i], lines[j])
Add Comment
Please, Sign In to add comment