Advertisement
ppupil2

PRF192_workshop-7

Mar 31st, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. void menu(void);
  9. bool input(char a[]);
  10. int countword(char a[]);
  11. void splitword(char a[]);
  12. void rmv_space(char a[]);
  13. void properform (char a[]);
  14. bool checkname(char a[]);
  15.  
  16. int main() {
  17.     int choice;
  18.     char a[91], b[91], ch;
  19.     bool inputcheck = false;
  20.    
  21.     while (1) {
  22.         while (1) {
  23.             system("cls");
  24.             menu();
  25.             printf("                     Enter your selection (0 -> 5): ");
  26.             fflush(stdin);
  27.             ch = '\0';
  28.             scanf("%d%c", &choice, &ch);
  29.             printf("\n");
  30.             if (choice >= 0 && choice <= 5 && ch == '\n') {
  31.                 break;
  32.             }
  33.             printf("                     Your selection is incorrect\n");
  34.             printf("\n");
  35.             printf("           Press any key to continue...");
  36.             getch();
  37.         }
  38.        
  39.        
  40.         if (choice == 0) { // get out while(1) if choice == '0'
  41.             break;
  42.         }
  43.         switch (choice) {
  44.             case 1: {
  45.                 inputcheck = input(a);
  46.                 break;
  47.             }
  48.             case 2: {
  49.                 if (inputcheck == false) {
  50.                     printf("           Please input first!\n");
  51.                 }
  52.                 else {
  53.                     printf("           The original string: ");
  54.                     puts(a);
  55.                     printf("           It has %d words, there are:\n", countword(a));
  56.                     splitword(a);
  57.                 }
  58.                
  59.                 break;
  60.             }
  61.             case 3: {
  62.                 if (inputcheck == false) {
  63.                     printf("           Please input first!\n");
  64.                 }
  65.                 else properform(a);
  66.                 break;
  67.             }
  68.             case 4: {
  69.                 if (inputcheck == false) {
  70.                     printf("           Please input first!\n");
  71.                 }
  72.                 else {
  73.                     printf("           The original string: ");
  74.                     puts(a);
  75.                     strcpy(b, a);
  76.                     rmv_space(b);
  77.                     printf("           The cleaned form: ");   
  78.                     puts(b);
  79.                 }
  80.                 break;
  81.             }
  82.             case 5: {
  83.                 if (inputcheck == false) {
  84.                     printf("           Please input first!\n");
  85.                 }
  86.                 else {
  87.                     bool x = checkname(a);
  88.                     if ( x == true) {
  89.                         printf("           The string '%s' is a valid name\n", a);
  90.                     }
  91.                     else {
  92.                         printf("           The string '%s' is not a valid name\n", a);
  93.                     }
  94.                 }
  95.                 break;
  96.             }
  97.         }
  98.         printf("\n");
  99.         printf("           Press any key to continue...");
  100.         getch();
  101.     }
  102.     return (0);
  103. }
  104.  
  105. void menu(void) {
  106.     printf("                     +-----------------------------------------+\n");
  107.     printf("                     |             Words Processing            |\n");
  108.     printf("                     +-----------------------------------------+\n");
  109.     printf("                     | 1. Input a string                       |\n");
  110.     printf("                     | 2. Split string into words              |\n");
  111.     printf("                     | 3. Convert a string into proper form    |\n");
  112.     printf("                     | 4. Remove redundant spaces              |\n");
  113.     printf("                     | 5. Check valid name                     |\n");
  114.     printf("                     | 0. Exit                                 |\n");
  115.     printf("                     +-----------------------------------------+\n\n");
  116. }
  117.  
  118. bool input(char a[]) {
  119.     bool check = false;
  120.    
  121.     while (1) {
  122.         printf("           Input a string: ");
  123.         fflush(stdin); strcpy(a, "\0");
  124.         scanf("%[^\n]", a);
  125.         if (strlen(a)>0 && strlen(a)<=90) {
  126.             check = true;
  127.             break;
  128.         }
  129.         else if (strlen(a) == 0) {
  130.             printf("           Invalid string, please re-enter.\n");
  131.         }
  132.         else {
  133.             printf("           String too long, please re-enter (Max = 90).\n");
  134.         }
  135.     }
  136.     return (check);
  137. }
  138.  
  139. int countword(char a[]) {
  140.     int count = 0;
  141.    
  142.     if (a[0] != ' ') {
  143.         count++;
  144.     }
  145.     for (int i = 0; i <= (strlen(a)-2); i++) {
  146.         if (a[i] == ' ' && a[i+1] != ' ') {
  147.             count++;
  148.         }
  149.     }
  150.     return (count);
  151. }
  152.  
  153. void splitword(char a[]) {
  154.     char b[91], *token;
  155.    
  156.     strcpy(b, a);
  157.     token = strtok(b, " ,.?\t");
  158.     while (token != '\0') {
  159.         printf("           \t");
  160.         puts(token);
  161.         token = strtok(NULL," ,.?\t");
  162.     }
  163. }
  164.  
  165. void rmv_space(char b[]) {
  166.     int x;
  167.    
  168.     /* Remove redundant spaces */
  169.     x = strlen(b);
  170.     for (int i = 0; i <= (x-2); i++) {  /* Replace \t by space */
  171.         if (b[i] == '\t') {
  172.             b[i] = ' ';
  173.         }
  174.     }
  175.     for (int i = 0; i <= (x-2); i++) {
  176.         if (b[0] == ' ') { /* Clear first space in b[0] */
  177.             for (int j = 0; j <= (x-2); j++) {
  178.                 b[j] = b[j+1];
  179.             }
  180.             b[x-1] = '\0';
  181.             x--;
  182.             i--;
  183.         }
  184.         if (b[i] == ' ' && b[i+1] == ' ') { /* Clear double space */
  185.             for (int j = i+1; j <= (x-2); j++) {
  186.                 b[j] = b[j+1];
  187.             }
  188.             b[x-1] = '\0';
  189.             x--;
  190.             i--;
  191.         }
  192.         if (b[x-1] == ' ') { /* Clear last space in b[0] */
  193.             b[x-1] = '\0';
  194.             x--;
  195.         }
  196.     }
  197. }
  198.  
  199. void properform(char a[]) {
  200.     char b[91];
  201.    
  202.     strcpy(b, a);
  203.     rmv_space(b);
  204.     if (islower(b[0])) {
  205.         b[0] = toupper(b[0]);
  206.     }
  207.     for (int i = 0; i <= strlen(b)-2; i++) {
  208.         if (b[i] == ' ' && islower(b[i+1])) {
  209.             b[i+1] = toupper(b[i+1]);
  210.             i++;
  211.         }
  212.     }
  213.    
  214.     printf("           The original string: ");
  215.     puts(a);
  216.     printf("           The proper form: ");
  217.     puts(b);
  218. }
  219.  
  220. bool checkname(char a[]) {
  221.     for (int i = 0; i < strlen(a); i++) {
  222.         if (isalpha(a[i]) == false && a[i] != ' ') {
  223.             return (false);
  224.         }
  225.     }
  226.     return (true);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement