Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*Defining the max value of a string*/
- #define MAX 50
- /*enums for boolean, and for the contract function's states*/
- enum boolean { FALSE, TRUE };
- enum state { OUT, CHECK, IN };
- /*Forward declaration of the contract function*/
- void contract(char s1[], char s2[]);
- /*Main function*/
- int main()
- {
- /*Declarations of variables used in main*/
- int c;
- int index = 0;
- int validString = TRUE;
- char s1[MAX] = {'\0'};
- char s2[MAX] = {'\0'};
- /*Telling the user what to do*/
- printf("\nPlease enter a string and press enter:\n");
- /*Loop for handling user input and storing it*/
- while ((c = getchar()) != '\n')
- {
- if (index >= MAX)
- {
- printf("Array limit has been reached.");
- validString = FALSE;
- break;
- }
- else
- {
- s1[index] = c;
- index++;
- }
- }
- /*Execute contract function and print the result*/
- if (validString)
- {
- contract(s1, s2);
- printf("\nManipulated string is:\n%s\n", s2);
- }
- return 0;
- }
- void contract(char s1[], char s2[])
- {
- /*Declarations of variables used in the contract function*/
- int state = OUT;
- int index = 0;
- int index2 = 0;
- /*Loop that ends when it finds a null character in the original string*/
- while (s1[index] != '\0')
- {
- /*Handle the "OUT" state*/
- if (state == OUT)
- {
- s2[index2] = s1[index];
- index2++;
- if (s1[index+1] == s1[index]+1)
- {
- state = CHECK;
- }
- }
- /*Handle the "CHECK" state*/
- else if (state == CHECK)
- {
- if (s1[index+1] == s1[index]+1)
- {
- state = IN;
- }
- else
- {
- s2[index2] = s1[index];
- index2++;
- }
- }
- /*Handle the "IN" state*/
- else if (state == IN)
- {
- if (s1[index+1] != s1[index]+1)
- {
- s2[index2] = '-';
- index2++;
- s2[index2] = s1[index];
- index2++;
- state = OUT;
- }
- }
- /*Increment the first index by one.*/
- index++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement