Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include "util/task_helper.h"
- #include<ctype.h>
- #define MAX 5000
- /*functie care transforma literele mari din interiorul versurilor in litere mici
- si prima litera din fiecare vers in litera mare*/
- void uppercase(char *s){
- int i;
- /*Pornesc de la al doilea caracter din sir,
- pe primul il voi prelucra la sfarsitul functiei*/
- for(i=1; s[i]!='\0'; i++) {
- /*In cazul in care caracterul anterior este diferit de newline
- si caracterul curent este litera mare, transform caracterul in litera mica*/
- if(s[i-1]!='\n' && (s[i]>= 'A' && s[i]<='Z'))
- s[i]=s[i]+32;
- /*In cazul in care caracterul curent este newline, verific daca urmatorul
- caracter este litera mica; daca este, il transform in litera mare*/
- if(s[i]=='\n'){
- if(s[i+1]>='a' && s[i+1]<='z')
- s[i+1]=s[i+1]-32;
- }
- }
- /*Daca primul caracter din sir este litera mica,
- atunci il transform in litera mare*/
- if(s[0]>='a' && s[0]<='z')
- s[0]=s[0]-32;
- }
- void trimming (char *s) {
- int i,j;
- char sir[7]=".,;:!?";
- for ( i = 0; s[i] != '\0'; i++){
- if ( strchr(sir,s[i]) != 0 ){
- for(j=i;s[j]!='\0';j++){
- s[j]=s[j+1];}
- i--;}
- }
- for( i = 0; s[i] != '\0'; i++ ) {
- while ( s[i]==' ' && s[i+1] == ' ' ) {
- for( j = i+1; s[j] != '\0' ; j++)
- s[j]=s[j+1];
- }
- }
- }
- void make_it_silly(char *s, char prob[10]) {
- float sample;
- int i;
- for(i=0; s[i]!='\0'; i++) {
- if((s[i]>='a' && s[i]<='z')||(s[i]>='A' && s[i]<='Z')) {
- if(s[i]>= 'A' && s[i]<='Z') {
- sample = rand() % 100 / 99.0;
- if(sample<atof(prob))
- s[i]=s[i]+32;
- }
- else {
- if(s[i]>= 'a' && s[i]<='z') {
- sample = rand() % 100 / 99.0;
- if(sample<atof(prob))
- s[i]=s[i]-32;
- }
- }
- }
- }
- }
- void make_it_friendly(char *sir)
- {
- int i,j,k,ap[MAX],cuvant=0,m ,pos;
- char s[MAX],cuv[MAX],poem[MAX],copie[MAX];
- strcpy(copie, sir);
- strcat(s, "");
- char *token=strtok(sir,";.:,?! \n");
- pos = 0;
- while(token != NULL) {
- char *dim=NULL;
- int a = strlen(token);
- get_friendly_word(token,&dim);
- if(dim != NULL) {
- strcat(s, dim);
- }
- else
- strcat(s, token);
- pos += strlen(token);
- while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
- s[strlen(s)] = copie[pos];
- pos++;
- }
- token=strtok(NULL,";.:,?! \n");
- }
- strcpy(sir, s);
- return;
- }
- char* get_replacer(char * word, char * rhyme, char * replace) {
- int n, i;
- char **wordlist;
- wordlist = malloc(100 * sizeof(int *));
- for(i = 0; i < 100; i++)
- wordlist[i]= malloc(100 * sizeof(char));
- get_synonym(word, &n, &wordlist);
- char lastch = rhyme[strlen(rhyme) - 1];
- if(wordlist != NULL)
- {
- int ok = 0;
- for(int j = 0; j < n; j++)
- {
- int lenght = strlen(wordlist[j]);
- char a, b;
- if(!toupper(wordlist[j][lenght - 1]));
- a = toupper(wordlist[j][lenght - 1]);
- if(!toupper(lastch));
- b = toupper(lastch);
- if(a==b) {
- if(!ok) {
- ok = 1;
- strcpy(replace, wordlist[j]);
- }
- else if(strcmp(replace, wordlist[j]) > 0 )
- {
- strcpy(replace, wordlist[j]);
- }
- }
- }
- }
- else
- strcpy(replace, "");
- }
- void make_it_rhyme(char *sir)
- {
- char lastwords[100][100], **wordlist, synlist[100][100];
- char s[5000];
- strcpy(s, "");
- char copie[MAX];
- strcpy(copie, sir);
- int *n,i;
- char *token=strtok(sir,";.:,?! \n");
- int pos = 0, cnt = 0;
- while(token != NULL) {
- pos += strlen(token);
- while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
- if(copie[pos] == '\n')
- {
- strcpy(lastwords[cnt++], token);
- }
- pos++;
- }
- if(pos>=strlen(copie))
- {
- strcpy(lastwords[cnt++], token);
- }
- token=strtok(NULL,";.:,?! \n");
- }
- char rhyme[101], replace[101];
- scanf("%s", rhyme);
- if(!strcmp(rhyme, "imperecheata")) {
- for(i = 0; i < cnt; i+=4)
- {
- get_replacer(lastwords[i], lastwords[i + 1], replace);
- if(strcmp(replace, "")) {
- strcpy(synlist[i], replace);
- strcpy(synlist[i + 1], lastwords[i+1]);
- }
- else
- {
- get_replacer(lastwords[i + 1], lastwords[i] , replace);
- strcpy(synlist[i], lastwords[i]);
- strcpy(synlist[i + 1], replace);
- }
- get_replacer(lastwords[i + 2], lastwords[i + 3] ,replace);
- if(strcmp(replace, "")) {
- strcpy(synlist[i + 2], replace);
- strcpy(synlist[i + 3], lastwords[i + 3]);
- }
- else
- {
- get_replacer(lastwords[i + 3], lastwords[i + 2], replace);
- strcpy(synlist[i + 2], lastwords[i + 2]);
- strcpy(synlist[i + 3], replace);
- }
- }
- }
- if(!strcmp(rhyme, "incrucisata")) {
- get_replacer(lastwords[i], lastwords[i + 2], replace);
- if(strcmp(replace, "")) {
- strcpy(synlist[i],replace);
- strcpy(synlist[i + 2], lastwords[i + 2]);
- }
- else
- {
- get_replacer(lastwords[i + 2], lastwords[i], replace);
- strcpy(synlist[i], lastwords[i]);
- strcpy(synlist[i + 2], replace);
- }
- get_replacer(lastwords[i + 1],lastwords[i + 3], replace);
- if(strcmp(replace, "")) {
- strcpy(synlist[i + 1], replace);
- strcpy(synlist[i + 3], lastwords[i + 3]);
- }
- else
- {
- get_replacer(lastwords[i + 3], lastwords[i + 1], replace);
- strcpy(synlist[i +1], lastwords[i + 1]);
- strcpy(synlist[i + 3], replace);
- }
- }
- if(!strcmp(rhyme, "imbratisata")) {
- get_replacer(lastwords[i], lastwords[i + 3], replace);
- if(strcmp(replace, "")) {
- strcpy(synlist[i], replace);
- strcpy(synlist[i + 3], lastwords[i + 3]);
- }
- else
- {
- get_replacer(lastwords[i + 3], lastwords[i], replace);
- strcpy(synlist[i], lastwords[i]);
- strcpy(synlist[i + 3], replace);
- }
- get_replacer(lastwords[i + 1],lastwords[i + 2], replace);
- if(strcmp(replace, "")) {
- strcpy(synlist[i + 1], replace);
- strcpy(synlist[i + 2], lastwords[i + 2]);
- }
- else
- {
- get_replacer(lastwords[i + 2], lastwords[i + 1], replace);
- strcpy(synlist[i +1], lastwords[i + 1]);
- strcpy(synlist[i + 2], replace);
- }
- }
- strcpy(sir, copie);
- token=strtok(sir,";.:,?! \n");
- pos = 0;
- cnt = 0;
- int where = 0;
- while(token != NULL) {
- pos += strlen(token);
- where = pos;
- int ok = 0;
- while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
- if(copie[pos] == '\n')
- {
- ok = 1;
- break;
- }
- pos++;
- }
- if(pos>=strlen(copie))
- {
- ok = 1;
- }
- if(ok == 1)
- strcat(s, synlist[cnt++]);
- else
- strcat(s, token);
- pos = where;
- while(pos < strlen(copie) && strchr(";.:,?! \n", copie[pos])) {
- s[strlen(s) ] = copie[pos];
- pos++;
- }
- token=strtok(NULL,";.:,?! \n");
- }
- strcpy(sir, s);
- }
- void print(char *s) {
- printf("%s\n\n", s);
- }
- //void rhimy( char *s) {
- int main()
- {
- char sir[100],poem[5000];
- char cale[100];
- char prob[10];
- char cuvant[100];
- srand(42);
- while(1) {
- scanf("%s", sir);
- if(strcmp(sir,"load")==0){
- scanf("%s", cale);
- load_file(cale,poem);
- }
- if(strcmp(sir,"uppercase")==0)
- uppercase(poem);
- if(strcmp(sir,"make_it_silly")==0){
- scanf("%s", prob);
- make_it_silly(poem, prob);
- }
- if(strcmp(sir,"print")==0)
- print(poem);
- if(strcmp(sir,"quit")==0)
- break;
- if(strcmp(sir,"trimming")==0)
- trimming(poem);
- if(strcmp(sir,"make_it_friendlier")==0)
- make_it_friendly(poem);
- if(strcmp(sir,"make_it_rhyme")==0)
- make_it_rhyme(poem);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement