Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. /*Defining the max value of a string*/
  5. #define MAX 50
  6.  
  7. /*enums for boolean, and for the contract function's states*/
  8. enum boolean { FALSE, TRUE };
  9. enum state { OUT, CHECK, IN };
  10.  
  11. /*Forward declaration of the contract function*/
  12. void contract(char s1[], char s2[]);
  13.  
  14. /*Main function*/
  15. int main()
  16. {
  17.     /*Declarations of variables used in main*/
  18.     int c;
  19.     int index = 0;
  20.     int validString = TRUE;
  21.     char s1[MAX] = {'\0'};
  22.     char s2[MAX] = {'\0'};
  23.    
  24.     /*Telling the user what to do*/
  25.     printf("\nPlease enter a string and press enter:\n");
  26.    
  27.     /*Loop for handling user input and storing it*/
  28.     while ((c = getchar()) != '\n')
  29.     {
  30.         if (index >= MAX)
  31.         {
  32.             printf("Array limit has been reached.");
  33.             validString = FALSE;
  34.             break;
  35.         }
  36.         else
  37.         {
  38.             s1[index] = c;
  39.             index++;
  40.         }
  41.     }
  42.    
  43.     /*Execute contract function and print the result*/
  44.     if (validString)
  45.     {
  46.             contract(s1, s2);
  47.             printf("\nManipulated string is:\n%s\n", s2);
  48.     }
  49.  
  50.     return 0;
  51. }
  52.  
  53. void contract(char s1[], char s2[])
  54. {
  55.     /*Declarations of variables used in the contract function*/
  56.     int state = OUT;
  57.     int index = 0;
  58.     int index2 = 0;
  59.    
  60.     /*Loop that ends when it finds a null character in the original string*/
  61.     while (s1[index] != '\0')
  62.     {
  63.         /*Handle the "OUT" state*/
  64.         if (state == OUT)
  65.         {
  66.             s2[index2] = s1[index];
  67.             index2++;
  68.             if (s1[index+1] == s1[index]+1)
  69.             {
  70.                 state = CHECK;
  71.             }  
  72.         }
  73.        
  74.         /*Handle the "CHECK" state*/       
  75.         else if (state == CHECK)
  76.         {
  77.             if (s1[index+1] == s1[index]+1)
  78.             {
  79.                 state = IN;
  80.             }
  81.             else
  82.             {
  83.                 s2[index2] = s1[index];
  84.                 index2++;
  85.             }
  86.         }
  87.        
  88.         /*Handle the "IN" state*/      
  89.         else if (state == IN)
  90.         {
  91.             if (s1[index+1] != s1[index]+1)
  92.             {
  93.                 s2[index2] = '-';
  94.                 index2++;
  95.                 s2[index2] = s1[index];
  96.                 index2++;
  97.                 state = OUT;
  98.             }  
  99.         }  
  100.        
  101.         /*Increment the first index by one.*/
  102.         index++;
  103.     }  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement