tsounakis

2.1 stable

Apr 17th, 2020
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 30
  5. #define M 100
  6.  
  7. char DEVELOPMENT[] = "";
  8.  
  9. void cleanString(char str[N]);
  10. char getchoice();
  11. void read(char a[M][N], char endword[], int *plength);
  12. void loaddict();
  13. void correct();
  14. void save();
  15. void stats(char a[M][N], int length);
  16. void legend();
  17.  
  18. int main() {
  19.     char choice, text[M][N], endword[] = "end", dict[M][N];
  20.     int length = 0;
  21.     int *plength;
  22.     plength = &length;
  23.     if (strcmp(DEVELOPMENT, "ON") == 0)
  24.     {
  25.         printf("DEVELOPMENT MODE[ON]\n");
  26.     }
  27.     legend();
  28.     while(1)
  29.     {      
  30.         printf("CHOOSE AN ACTION: ");
  31.         choice = getchoice();
  32.         switch (choice)
  33.         {
  34.             case 'r':
  35.                 read(text, endword, plength);
  36.                 break;
  37.             case 'd':
  38.                 loaddict(dict, endword);
  39.                 break;
  40.             case 'c':
  41.                 correct();
  42.                 break;
  43.             case 's':
  44.                 save();
  45.                 break;
  46.             case 'S':
  47.                 stats(text, length);
  48.                 break;
  49.             case 'l':
  50.                 legend();
  51.                 break;
  52.             case 'e':
  53.                 return 0;
  54.                 break;
  55.             default:
  56.                 printf("\n*Wrong input*\n");
  57.                 break;
  58.         }
  59.         putchar('\n');
  60.     }
  61. }
  62. char getchoice()
  63. {
  64.     char choice;
  65.     scanf(" %c", &choice);
  66.     return choice;
  67. }
  68.  
  69. void read(char a[M][N], char endword[], int *plength)
  70. {
  71.     char test[N];
  72.     int i;
  73.     i=0;
  74.     printf("(READ TEXT MODE[ON]):~ *INITIATING READ TEXT MODE*\n");
  75.     while(1)
  76.     {
  77.         while (1)
  78.         {  
  79.             printf("Type: ");
  80.             scanf(" %s", test);
  81.             if (strcpy(DEVELOPMENT, "ON") == 0)
  82.             {
  83.                 printf("%d\n", strlen(test));
  84.             }
  85.             if (strlen(test) >= N)
  86.             {
  87.                 printf("\nError: possible buffer overflow due to the length of the typed word. Please retype.\n");
  88.             }
  89.             else
  90.             {
  91.                 break;
  92.             }
  93.         }
  94.         if (strcmp(test, endword) == 0)
  95.         {
  96.             break;
  97.         }
  98.         strcpy(a[i], test);
  99.         i++;
  100.     }
  101.     *plength = i;
  102.     printf("(READ TEXT MODE[OFF]...):~ \n");
  103.     return;
  104. }
  105.  
  106. void loaddict()
  107. {
  108.     printf("\nDummy function.\n");
  109.     return;
  110. }
  111.  
  112. void correct()
  113. {
  114.     printf("\nText corrected.\n\n");
  115.     return;
  116. }
  117.  
  118. void save()
  119. {
  120.     printf("\nText saved.\n\n");
  121.     return;
  122. }
  123.  
  124. void cleanString(char str[N])
  125. {
  126.     int i, j;
  127.     for(i=0; str[i]!='\0'; ++i)
  128.     {
  129.         while (!((str[i]>='a'&&str[i]<='z') || ((str[i]>='A' && str[i]<='Z') || str[i]=='\0')))
  130.         {
  131.             for(j=i;str[j]!='\0';++j)
  132.             {
  133.                 str[j]=str[j+1];
  134.             }
  135.             str[j]='\0';
  136.         }
  137.     }
  138.     return;
  139. }
  140.  
  141. void stats(char a[M][N], int length)
  142. {
  143.     char choice, da[M][N], old[M][N];
  144.     int lengthWords, lengthChars = 0, i, j, stop = 0, diff = 0, occ, occA[M] = {0}, k;
  145.     for (i=0; i<length; i++)
  146.     {
  147.         cleanString(a[i]);
  148.     }
  149.     for (i=0; i<length; i++)
  150.     {
  151.         strcpy(old[i], a[i]);
  152.         strupr(a[i]);
  153.     }
  154.     printf("(STATS MODE[ON]):~ *INITIATING STATS MODE*\n");
  155.     if (strlen(a[0]) == 0)
  156.     {
  157.         printf("NON READABLE TEXT.\n");
  158.         return;
  159.     }
  160.     for (i=0; i < length; i++)
  161.     {
  162.         for (j=0; old[i][j] != '\0'; j++)
  163.         {
  164.             lengthChars++;
  165.         }
  166.     }
  167.     lengthWords = i;
  168.     for (i=0; i < length; i++)
  169.     {
  170.         occ = 1;
  171.         for (j=i; j<length;j++)
  172.         {
  173.             if (strcmp(a[i], "*0*0*0*") != 0)
  174.             {
  175.                 if (strcmp(a[i], a[j]) == 0 && i != j)
  176.                 {
  177.                     occ++;
  178.                     strcpy(a[j], "*0*0*0*");
  179.                 }
  180.                 if (j == length - 1)
  181.                 {
  182.                     strcpy(da[diff], old[i]);
  183.                     occA[diff] = occ;
  184.                     diff++;
  185.                 }
  186.             }  
  187.         }
  188.     }
  189.     while (stop == 0)
  190.     {
  191.         printf("\nCHOOSE [a, b or c]:");
  192.         scanf(" %c", &choice);
  193.         switch(choice){
  194.             case 'a':
  195.                 printf("The text is composed of %d words and %d characters (%d including the spaces).\n", lengthWords, lengthChars, lengthChars + lengthWords - 1);
  196.                 break;
  197.             case 'b':
  198.                 printf("The text is composed of %d different words.\n", diff);
  199.                 break;
  200.             case 'c':
  201.                 printf("Word:\tOccurence:\t\n");
  202.                 for (i=0; i<length; i++)
  203.                 {
  204.                     for (j=0; j<length; j++)
  205.                     {
  206.                         if (i==occA[j])
  207.                         {
  208.                             printf("%s\t:\t", da[j]);
  209.                             for (k=0; k<occA[j];k++)
  210.                             {
  211.                                 printf("*");
  212.                             }
  213.                             putchar('\n');
  214.                         }
  215.                     }
  216.                 }
  217.                 break;
  218.             case 'e':
  219.                 stop = 1;
  220.             default:
  221.                 break;
  222.         }
  223.     }  
  224.     printf("(STATS MODE[OFF]):~ ...\n");
  225.     return;
  226. }
  227.  
  228. void legend()
  229. {
  230.     printf("LEGEND:\n[r:\tInsert text]\n[d:\tLoad dictionary]\n[c:\tCorrect the loaded text]\n[S:\tStatistics]\n[s:\tSave text]\n[l:\tShow legend]\n[e:\tEscape]\n");
  231.     return;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment