Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include "util/task_helper.h"
  5. #include<ctype.h>
  6. #define MAX 5000
  7.  
  8. /*functie care transforma literele mari din interiorul versurilor in litere mici
  9. si prima litera din fiecare vers in litera mare*/
  10. void uppercase(char *s){
  11.  
  12. int i;
  13. /*Pornesc de la al doilea caracter din sir,
  14. pe primul il voi prelucra la sfarsitul functiei*/
  15. for(i=1; s[i]!='\0'; i++) {
  16. /*In cazul in care caracterul anterior este diferit de newline
  17. si caracterul curent este litera mare, transform caracterul in litera mica*/
  18. if(s[i-1]!='\n' && (s[i]>= 'A' && s[i]<='Z'))
  19. s[i]=s[i]+32;
  20. /*In cazul in care caracterul curent este newline, verific daca urmatorul
  21. caracter este litera mica; daca este, il transform in litera mare*/
  22. if(s[i]=='\n'){
  23. if(s[i+1]>='a' && s[i+1]<='z')
  24. s[i+1]=s[i+1]-32;
  25. }
  26. }
  27. /*Daca primul caracter din sir este litera mica,
  28. atunci il transform in litera mare*/
  29. if(s[0]>='a' && s[0]<='z')
  30.  
  31. s[0]=s[0]-32;
  32. }
  33.  
  34.  
  35. void trimming (char *s) {
  36. int i,j;
  37. char sir[7]=".,;:!?";
  38. for ( i = 0; s[i] != '\0'; i++){
  39. if ( strchr(sir,s[i]) != 0 ){
  40. for(j=i;s[j]!='\0';j++){
  41. s[j]=s[j+1];}
  42. i--;}
  43. }
  44. for( i = 0; s[i] != '\0'; i++ ) {
  45. while ( s[i]==' ' && s[i+1] == ' ' ) {
  46. for( j = i+1; s[j] != '\0' ; j++)
  47. s[j]=s[j+1];
  48. }
  49. }
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. void make_it_silly(char *s, char prob[10]) {
  60. float sample;
  61. int i;
  62.  
  63. for(i=0; s[i]!='\0'; i++) {
  64. if((s[i]>='a' && s[i]<='z')||(s[i]>='A' && s[i]<='Z')) {
  65.  
  66. if(s[i]>= 'A' && s[i]<='Z') {
  67. sample = rand() % 100 / 99.0;
  68. if(sample<atof(prob))
  69. s[i]=s[i]+32;
  70.  
  71. }
  72.  
  73. else {
  74. if(s[i]>= 'a' && s[i]<='z') {
  75.  
  76. sample = rand() % 100 / 99.0;
  77. if(sample<atof(prob))
  78. s[i]=s[i]-32;
  79. }
  80. }
  81.  
  82.  
  83. }
  84. }
  85.  
  86. }
  87.  
  88. void make_it_friendly(char *sir)
  89. {
  90. int i,j,k,ap[MAX],cuvant=0,m ,pos;
  91. char s[MAX],cuv[MAX],poem[MAX],copie[MAX];
  92. strcpy(copie, sir);
  93. strcat(s, "");
  94. char *token=strtok(sir,";.:,?! \n");
  95. pos = 0;
  96. while(token != NULL) {
  97. char *dim=NULL;
  98. int a = strlen(token);
  99. get_friendly_word(token,&dim);
  100. if(dim != NULL) {
  101. strcat(s, dim);
  102. }
  103. else
  104. strcat(s, token);
  105. pos += strlen(token);
  106. while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
  107. s[strlen(s)] = copie[pos];
  108. pos++;
  109. }
  110. token=strtok(NULL,";.:,?! \n");
  111. }
  112. strcpy(sir, s);
  113. return;
  114.  
  115. }
  116. char* get_replacer(char * word, char * rhyme, char * replace) {
  117. int n, i;
  118. char **wordlist;
  119. wordlist = malloc(100 * sizeof(int *));
  120. for(i = 0; i < 100; i++)
  121. wordlist[i]= malloc(100 * sizeof(char));
  122. get_synonym(word, &n, &wordlist);
  123. char lastch = rhyme[strlen(rhyme) - 1];
  124. if(wordlist != NULL)
  125. {
  126. int ok = 0;
  127. for(int j = 0; j < n; j++)
  128. {
  129. int lenght = strlen(wordlist[j]);
  130. char a, b;
  131. if(!toupper(wordlist[j][lenght - 1]));
  132. a = toupper(wordlist[j][lenght - 1]);
  133. if(!toupper(lastch));
  134. b = toupper(lastch);
  135. if(a==b) {
  136. if(!ok) {
  137. ok = 1;
  138. strcpy(replace, wordlist[j]);
  139.  
  140. }
  141. else if(strcmp(replace, wordlist[j]) > 0 )
  142. {
  143. strcpy(replace, wordlist[j]);
  144. }
  145. }
  146. }
  147. }
  148. else
  149. strcpy(replace, "");
  150. }
  151. void make_it_rhyme(char *sir)
  152. {
  153. char lastwords[100][100], **wordlist, synlist[100][100];
  154. char s[5000];
  155. strcpy(s, "");
  156. char copie[MAX];
  157. strcpy(copie, sir);
  158. int *n,i;
  159. char *token=strtok(sir,";.:,?! \n");
  160. int pos = 0, cnt = 0;
  161. while(token != NULL) {
  162. pos += strlen(token);
  163.  
  164. while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
  165. if(copie[pos] == '\n')
  166. {
  167. strcpy(lastwords[cnt++], token);
  168.  
  169. }
  170. pos++;
  171.  
  172. }
  173. if(pos>=strlen(copie))
  174. {
  175. strcpy(lastwords[cnt++], token);
  176. }
  177. token=strtok(NULL,";.:,?! \n");
  178. }
  179. char rhyme[101], replace[101];
  180. scanf("%s", rhyme);
  181. if(!strcmp(rhyme, "imperecheata")) {
  182. for(i = 0; i < cnt; i+=4)
  183. {
  184. get_replacer(lastwords[i], lastwords[i + 1], replace);
  185. if(strcmp(replace, "")) {
  186. strcpy(synlist[i], replace);
  187. strcpy(synlist[i + 1], lastwords[i+1]);
  188. }
  189. else
  190. {
  191. get_replacer(lastwords[i + 1], lastwords[i] , replace);
  192. strcpy(synlist[i], lastwords[i]);
  193. strcpy(synlist[i + 1], replace);
  194. }
  195. get_replacer(lastwords[i + 2], lastwords[i + 3] ,replace);
  196. if(strcmp(replace, "")) {
  197. strcpy(synlist[i + 2], replace);
  198. strcpy(synlist[i + 3], lastwords[i + 3]);
  199. }
  200. else
  201. {
  202. get_replacer(lastwords[i + 3], lastwords[i + 2], replace);
  203. strcpy(synlist[i + 2], lastwords[i + 2]);
  204. strcpy(synlist[i + 3], replace);
  205. }
  206.  
  207. }
  208. }
  209. if(!strcmp(rhyme, "incrucisata")) {
  210. get_replacer(lastwords[i], lastwords[i + 2], replace);
  211. if(strcmp(replace, "")) {
  212. strcpy(synlist[i],replace);
  213. strcpy(synlist[i + 2], lastwords[i + 2]);
  214. }
  215. else
  216. {
  217. get_replacer(lastwords[i + 2], lastwords[i], replace);
  218. strcpy(synlist[i], lastwords[i]);
  219. strcpy(synlist[i + 2], replace);
  220. }
  221. get_replacer(lastwords[i + 1],lastwords[i + 3], replace);
  222. if(strcmp(replace, "")) {
  223. strcpy(synlist[i + 1], replace);
  224. strcpy(synlist[i + 3], lastwords[i + 3]);
  225. }
  226. else
  227. {
  228. get_replacer(lastwords[i + 3], lastwords[i + 1], replace);
  229. strcpy(synlist[i +1], lastwords[i + 1]);
  230. strcpy(synlist[i + 3], replace);
  231. }
  232. }
  233. if(!strcmp(rhyme, "imbratisata")) {
  234. get_replacer(lastwords[i], lastwords[i + 3], replace);
  235. if(strcmp(replace, "")) {
  236. strcpy(synlist[i], replace);
  237. strcpy(synlist[i + 3], lastwords[i + 3]);
  238. }
  239. else
  240. {
  241. get_replacer(lastwords[i + 3], lastwords[i], replace);
  242. strcpy(synlist[i], lastwords[i]);
  243. strcpy(synlist[i + 3], replace);
  244. }
  245. get_replacer(lastwords[i + 1],lastwords[i + 2], replace);
  246. if(strcmp(replace, "")) {
  247. strcpy(synlist[i + 1], replace);
  248. strcpy(synlist[i + 2], lastwords[i + 2]);
  249. }
  250. else
  251. {
  252. get_replacer(lastwords[i + 2], lastwords[i + 1], replace);
  253. strcpy(synlist[i +1], lastwords[i + 1]);
  254. strcpy(synlist[i + 2], replace);
  255. }
  256. }
  257. strcpy(sir, copie);
  258. token=strtok(sir,";.:,?! \n");
  259. pos = 0;
  260. cnt = 0;
  261. int where = 0;
  262. while(token != NULL) {
  263. pos += strlen(token);
  264. where = pos;
  265. int ok = 0;
  266. while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
  267. if(copie[pos] == '\n')
  268. {
  269. ok = 1;
  270. break;
  271. }
  272. pos++;
  273.  
  274. }
  275. if(pos>=strlen(copie))
  276. {
  277. ok = 1;
  278. }
  279. if(ok == 1)
  280. strcat(s, synlist[cnt++]);
  281. else
  282. strcat(s, token);
  283. pos = where;
  284. while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
  285. s[strlen(s) ] = copie[pos];
  286. pos++;
  287. }
  288.  
  289. token=strtok(NULL,";.:,?! \n");
  290. }
  291. strcpy(sir, s);
  292.  
  293. }
  294. void print(char *s) {
  295. printf("%s\n\n", s);
  296. }
  297.  
  298. //void rhimy( char *s) {
  299.  
  300.  
  301.  
  302.  
  303. int main()
  304. {
  305. char sir[100],poem[5000];
  306. char cale[100];
  307. char prob[10];
  308. char cuvant[100];
  309. srand(42);
  310.  
  311. while(1) {
  312. scanf("%s", sir);
  313. if(strcmp(sir,"load")==0){
  314. scanf("%s", cale);
  315. load_file(cale,poem);
  316. }
  317.  
  318. if(strcmp(sir,"uppercase")==0)
  319. uppercase(poem);
  320.  
  321. if(strcmp(sir,"make_it_silly")==0){
  322. scanf("%s", prob);
  323. make_it_silly(poem, prob);
  324. }
  325.  
  326. if(strcmp(sir,"print")==0)
  327. print(poem);
  328.  
  329.  
  330. if(strcmp(sir,"quit")==0)
  331. break;
  332. if(strcmp(sir,"trimming")==0)
  333. trimming(poem);
  334. if(strcmp(sir,"make_it_friendlier")==0)
  335. make_it_friendly(poem);
  336. if(strcmp(sir,"make_it_rhyme")==0)
  337. make_it_rhyme(poem);
  338.  
  339. }
  340.  
  341. return 0;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement