Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. char text[101], character[2];
  5. int i, j, choice = 0;
  6.  
  7. // Ignore extra spaces, count ALL characters
  8.  
  9. void showMenu()
  10. {
  11.     system("cls");
  12.     printf("---- MENU -----\n\n");
  13.     printf("1. Split text\n");
  14.     printf("2. Upper case to lower case\n");
  15.     printf("3. Lower case to upper case\n");
  16.     printf("4. Remove a character\n");
  17.     printf("5. Add a character\n");
  18.     printf("6. Replace a character\n");
  19.     printf("7. Statistics\n");
  20.     printf("8. Exit\n");
  21.     printf("\nYour choice: ");
  22. }
  23.  
  24. void flush()
  25. {
  26.     int c;
  27.     while ((c = getchar()) != '\n' && c != EOF);
  28. }
  29.  
  30. char enterText()
  31. {
  32.     flush();
  33.     printf("Enter your text (max 100 chars): ");
  34.     fgets(text, 100, stdin);
  35.     return text;
  36. }
  37.  
  38. void enterCharacter(int n)
  39. {
  40.     for (i = 0; i < n; i++)
  41.     {
  42.         scanf_s("%c", &character[i]);
  43.     }
  44. }
  45.  
  46. void flush();
  47.  
  48. int main()
  49. {
  50.     while (choice != 8)
  51.     {
  52.         int i = 0;
  53.         fseek(stdin, 0, SEEK_END);
  54.         showMenu();
  55.         scanf_s("%d", &choice);
  56.         printf("\n");
  57.  
  58.         switch (choice)
  59.         {
  60.         case 1:
  61.             enterText();
  62.             printf("\n");
  63.             while (text[i])
  64.             {
  65.                 (text[i] == ' ') ? putchar('\n') : putchar(text[i]);
  66.                 i++;
  67.             }
  68.             printf("\n");
  69.             break;
  70.         case 2:
  71.             enterText();
  72.             printf("\n");
  73.             while (text[i])
  74.             {
  75.                 putchar(tolower(text[i]));
  76.                 i++;
  77.             }
  78.             printf("\n");
  79.             break;
  80.         case 3:
  81.             enterText();
  82.             printf("\n");
  83.             while (text[i])
  84.             {
  85.                 putchar(toupper(text[i]));
  86.                 i++;
  87.             }
  88.             printf("\n");
  89.             break;
  90.         case 4:
  91.             enterText();
  92.             printf("Enter character to remove: ");
  93.             enterCharacter(1);
  94.             printf("\n");
  95.             while (text[i])
  96.             {
  97.                 if (text[i] != character[0])
  98.                     putchar(text[i]);
  99.                 i++;
  100.             }
  101.             printf("\n");
  102.             break;
  103.         case 5:
  104.             enterText();
  105.             printf("Enter character to add: ");
  106.             enterCharacter(1);
  107.             printf("\n");
  108.             while (text[i])
  109.             {
  110.                 switch (text[i])
  111.                 {
  112.                 case ' ':
  113.                     putchar(character[0]);
  114.                     putchar(' ');
  115.                     break;
  116.                 case '\n':
  117.                     putchar(character[0]);
  118.                     break;
  119.                 default:
  120.                     putchar(text[i]);
  121.                     break;
  122.                 }
  123.                 i++;
  124.             }
  125.             printf("\n");
  126.             break;
  127.         case 6:
  128.             enterText();
  129.             printf("Enter two characters (ex ab for replacing a with b): ");
  130.             enterCharacter(2);
  131.             printf("\n");
  132.             while (text[i])
  133.             {
  134.                 (text[i] == character[0]) ? putchar(character[1]) : putchar(text[i]);
  135.                 i++;
  136.             }
  137.             printf("\n");
  138.             break;
  139.         case 7:
  140.             enterText();
  141.             j = 1;
  142.             while (text[i])
  143.             {
  144.                 if (text[i] == ' ')
  145.                     j++;
  146.                 i++;
  147.             }
  148.             printf("\n");
  149.             printf("Number of words: %d\n", j);
  150.             int alphaCount[58] = { 0 };
  151.             for (int i = 0; i < strlen(text); i++)
  152.                 if (isalpha(text[i]))
  153.                     alphaCount[(text[i]) - 'A']++;
  154.             for (int i = 32; i <= 57; i++)
  155.                 (alphaCount[i] != 0) ? printf("%c : %d\n", i + 65, alphaCount[i]) : "";
  156.  
  157.             for (int i = 0; i <= 25; i++)
  158.                 (alphaCount[i] != 0) ? printf("%c : %d\n", i + 65, alphaCount[i]) : "";
  159.             printf("\n");
  160.             break;
  161.         case 8:
  162.             return 0;
  163.             break;
  164.         default:
  165.             printf("Not a valid option!\n\n");
  166.             break;
  167.         }
  168.         system("pause");
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement