Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "util/task_helper.h"
- #include <stdlib.h>
- #define CMD_MAX_LNGTH 100
- #define DELIMITERS " .,;?!:"
- void replace(char *poezie,char * new,char *old)
- {
- char *copie=(char *)malloc(4000*sizeof(char));
- strcpy(copie,poezie);
- char *poz=strstr(poezie,old);
- int flag=poz-poezie;
- poezie[flag]='\0';
- strcat(poezie,new);
- strcat(poezie,copie+flag+strlen(old));
- free(copie);
- }
- char to_low(char s)
- {
- if(s>='A'&&s<='Z')
- {
- s+=('a'-'A');
- return s;
- }else
- {
- return s;
- }
- }
- void rhyme_alg(char *cuv1, char *cuv2, char *dest)
- {
- char **lista;
- char *locatie=(char*)malloc(100*sizeof(char *)) ;
- int lngth1, lngth2,n,gasit=0,i,lngth_crt;
- lngth1=strlen(cuv1);
- lngth2=strlen(cuv2);
- //printf("%d %d ",lngth1,lngth2);
- //printf("%c %c ", cuv1[lngth1-1],cuv2[lngth2-1]);
- if(cuv1[lngth1-1]!=cuv2[lngth2-1])
- {
- get_synonym(cuv1,&n,&lista);
- //printf("%d", n);
- if(n==-1)
- {
- gasit=0;
- }else
- {
- for(i=0;i<n&&gasit==0;i++)
- {
- lngth_crt=strlen(lista[i]);
- //printf("%d\n", lngth_crt);
- //printf("%d ", gasit );
- //printf("%c ", lista[i][lngth_crt-1]);
- if((lista[i][lngth_crt-1]==cuv2[lngth2-1])||(lista[i][lngth_crt-1]+32==cuv2[lngth2-1]))
- {
- gasit=i;
- }
- }
- if(gasit!=0)
- {
- strcpy(locatie,lista[gasit]);
- }
- //printf("%d\n", gasit);
- }
- if(gasit!=0)
- {printf("*");
- replace(dest,locatie,cuv1);
- }
- if(gasit==0)
- {
- get_synonym(cuv2,&n,&lista);
- if(n==-1)
- {
- gasit=0;
- }else
- {
- for(i=0;i<n&&gasit==0;i++)
- {
- lngth_crt=strlen(lista[i]);
- if((lista[i][lngth_crt-1]==cuv1[lngth2-1])||(lista[i][lngth_crt-1]+32==cuv1[lngth2-1]))
- {
- gasit=i;
- }
- }
- if(gasit!=0)
- {
- strcpy(locatie,lista[gasit]);
- }
- if(gasit!=0)
- {printf("*");printf("%s\n",locatie);
- printf("%s",cuv2);
- replace(dest,locatie,cuv2);
- }
- }
- }
- }
- }
- int is_letter(char s)
- {
- if((s>='a'&&s<='z')||(s>='A'&&s<='Z'))
- {
- return 1;
- }
- return 0;
- }
- int is_valid(char s)
- {
- if(is_letter(s)==1)
- {
- return 1;
- }
- else
- {
- if(strstr(DELIMITERS,&s)==NULL)
- {
- return 1;
- }
- }
- return 0;
- }
- void upper_vers(char *vers)
- {
- int lngth_vers=strlen(vers),i;
- if(vers[0]>='a'&& vers[0]<='z')
- {
- vers[0]-=('a'-'A');
- }
- for(i=1;i<lngth_vers;i++)
- {
- if(vers[i]>='A'&& vers[i]<='Z')
- {
- vers[i]+=('a'-'A');
- }
- }
- }
- void uppercase(char *poezie)
- {
- char *vers,*copie=poezie;
- int lngth_poezie=strlen(poezie), i;
- vers=strtok(copie,"\n");
- upper_vers(vers);
- while(vers!=NULL)
- {
- vers=strtok(NULL,"\n");
- if(vers==NULL)
- {
- break;
- }
- upper_vers(vers);
- }
- for(i=0;i<lngth_poezie;i++)
- {
- if(poezie[i]=='\0')
- {
- poezie[i]='\n';
- }
- }
- }
- void trimming(char *poezie)
- {
- int lngth_poezie=strlen(poezie),i,poz=0;
- char *copie=(char *)malloc(lngth_poezie*sizeof(char));
- for(i=0;i<lngth_poezie;i++)
- {
- if(is_valid(poezie[i])==1)
- {
- copie[poz]=poezie[i];
- poz++;
- }
- else
- {
- if(poezie[i]==' '&& poezie[i-1]!=' ')
- {
- copie[poz]=poezie[i];
- poz++;
- }
- }
- }
- copie[poz]='\0';
- strcpy(poezie,copie);
- free(copie);
- }
- void silly(char *poezie,float prob)
- {
- float sample;
- int lngth_poezie=strlen(poezie),i;
- for(i=0;i<lngth_poezie;i++)
- {
- if(is_letter(poezie[i])==1)
- {
- sample = rand() % 100 / 99.0;
- if(sample<prob)
- {
- if(poezie[i]>='a'&&poezie[i]<='z')
- {
- poezie[i]-=('a'-'A');
- }
- else
- {
- if(poezie[i]>='A'&&poezie[i]<='Z')
- {
- poezie[i]+=('a'-'A');
- }
- }
- }
- }
- }
- }
- void rhimy(char *poezie, char *rima)
- {
- char *copie, *line, **cuv;
- int lngth_poezie=strlen(poezie);
- int lngth_line,i, j;
- int nr_cuv=0, l_cuv;
- cuv=(char **)malloc(80*sizeof(char *));
- for(i=0;i<80;i++)
- {
- cuv[i]=(char *)malloc(100*sizeof(char));
- }
- copie=(char *)malloc(lngth_poezie*sizeof(char));
- strcpy(copie,poezie);
- line=strtok(copie,"\n");
- lngth_line=strlen(line);
- for(i=lngth_line-1;i>=0;i--)
- {
- if(*(line+i)==' ')
- {
- strcpy(cuv[nr_cuv],line+i+1);
- break;
- }
- }
- nr_cuv++;
- while(line!=NULL)
- {
- line=strtok(NULL,"\n");
- if(line==NULL)
- {
- break;
- }
- lngth_line=strlen(line);
- for(i=lngth_line-1;i>=0;i--)
- {
- if(*(line+i)==' ')
- {
- strcpy(cuv[nr_cuv],line+i+1);
- break;
- }
- }
- nr_cuv++;
- }
- free(copie);
- for(i=0;i<nr_cuv;i++)
- {
- for(j=0;cuv[i][j]!='\0';j++)
- {
- if(is_letter(cuv[i][j])!=1)
- {
- cuv[i][j]='\0';
- }
- printf("%c", cuv[i][j]);
- }
- printf("\n");
- }
- if(strcmp(rima,"imperecheata")==0)
- {
- for(i=0;i<nr_cuv;i+=4)
- {
- rhyme_alg(cuv[i],cuv[i+1],poezie);
- rhyme_alg(cuv[i+2],cuv[i+3],poezie);
- }
- }
- }
- void friendly(char *poezie)
- {
- char *cuv, *poz,*copie;
- char **dim;
- int lngth_poezie=strlen(poezie);
- int lngth_dim, lngth_cuv;
- copie=(char *)malloc(lngth_poezie*sizeof(char));
- int lngth_diff=0;
- strcpy(copie,poezie);
- cuv=strtok(copie," \n.,:;?!");
- get_friendly_word(cuv,dim);
- if(*dim!=NULL)
- {
- lngth_cuv=strlen(cuv);
- lngth_dim=strlen(*dim);
- lngth_diff+=(lngth_dim- lngth_cuv);
- replace(poezie,*dim,cuv);
- }
- while(cuv!=NULL)
- {
- if(cuv==NULL)
- {
- break;
- }
- cuv=strtok(NULL," \n.,:;?!");
- get_friendly_word(cuv,dim);
- if(*dim!=NULL)
- {
- replace(poezie+(cuv-copie+lngth_diff),*dim,cuv);
- lngth_cuv=strlen(cuv);
- lngth_dim=strlen(*dim);
- lngth_diff+=(lngth_dim- lngth_cuv);
- }
- }
- }
- void print(char *poezie)
- {
- printf("%s\n\n",poezie);
- }
- int main(void)
- {
- srand(42);
- char cmd[CMD_MAX_LNGTH];
- int lngth,lngth_buff;
- char *buffer,*copie;
- buffer=(char *)malloc(3000*sizeof(char));
- while(1)
- {
- fgets(cmd,CMD_MAX_LNGTH,stdin);
- lngth=strlen(cmd);
- cmd[lngth-1]='\0';
- if(cmd[0]=='l')
- {
- load_file(cmd+5,buffer);
- }
- if(cmd[0]=='q')
- {
- break;
- }
- if(cmd[0]=='p')
- {
- print(buffer);
- }
- if(cmd[0]=='u')
- {
- uppercase(buffer);
- }
- if(cmd[0]=='t')
- {
- trimming(buffer);
- }
- if(strstr(cmd,"make_it_silly")!=NULL)
- {
- float prob=atof(cmd+13);
- silly(buffer,prob);
- }
- if(strstr(cmd,"make_it_friendlier")!=NULL)
- {
- friendly(buffer);
- }
- if(strstr(cmd,"make_it_rhyme")!=NULL)
- {
- rhimy(buffer,cmd+14);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement