Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void merge();
  5. int isSortedSet(char *set);
  6. int isCorrectSet(char *set);
  7. char * get(int n, char *str);
  8. void mergeSets(char *set1, char *set2, char *result);
  9. void myMergeSets(char * set1, char * set2, char * word1, char * word2, char * result, char * last, int flag1, int flag2);
  10.  
  11. void main () {
  12. merge();
  13. }
  14.  
  15. void merge ()
  16. {
  17. char str1[257], str2[257], str[257], res[514];
  18. strcpy(str1,get(1,str)); // get string with get function
  19. strcpy(str2,get(2,str));
  20. mergeSets(str1, str2, res);
  21. }
  22.  
  23. char * get(int n, char * str) { // get and check the strings
  24. if (n == 1)
  25. printf("Enter first set of names:\n");
  26. if (n == 2)
  27. printf("Enter second set of names:\n");
  28. gets(str);
  29. if (!isCorrectSet(str) || !isSortedSet(str))
  30. {
  31. printf("Input Incorrect! Please try again.\n");
  32. return get(n,str);
  33. }
  34. return str;
  35. }
  36.  
  37. int isCorrectSet(char *set) { //check if the string is legal
  38. if (*set=='\0')
  39. return 1;
  40. if (((*set<'a') || (*set>'z')) && (*set != ' '))
  41. return 0;
  42. return isCorrectSet(set+1);
  43. }
  44.  
  45. int isSortedSet(char *set) //check if the srting is sorted by using counter
  46. {
  47. char name1[257], name2[257], temp[257];
  48. int count1, count2;
  49. strcpy(temp, set);
  50. count1 = sscanf(temp, "%s %[^s]", name1, temp);
  51. count2 = sscanf(temp, "%s", name2);
  52. if ((count1 == 2) && count2)
  53. {
  54. if ((strcmp(name1, name2) > 0))
  55. return 0;
  56. return isSortedSet(temp);
  57. }
  58. else
  59. return 1;
  60. }
  61.  
  62. void mergeSets(char *set1, char *set2, char *result) {
  63. char last[257], word1[257], word2[257];
  64. result[0] = '\0'; // initialization the result string
  65. last[0] = 'A';
  66. sscanf(set1, "%s %[^$]", word1, set1); // read the first word
  67. sscanf(set2, "%s %[^$]", word2, set2);//
  68. myMergeSets(set1,set2,word1,word2,result,last,0,0); //Auxiliary function
  69. result[(strlen(result)-1)] = '\0';
  70. printf("Result:\n");
  71. puts(result);
  72. }
  73.  
  74. void myMergeSets(char * set1, char * set2, char * word1, char * word2, char * result, char * last, int flag1, int flag2)
  75. {
  76. if (!set1[0]) // check if set1 is empty
  77. flag1 = 1;
  78. if (!set2[0]) // check if set2 is empty
  79. flag2 = 1;
  80. if (flag1 && flag2)
  81. return;
  82. if (flag2 || (!flag1 && (strcmp(word1,word2) < 0))) // all cases to write word1 first
  83. {
  84. if (strcmp(last,word1) != 0)
  85. {
  86. strcat(result,word1);
  87. strcat(result," ");
  88. strcpy(last,word1);
  89. }
  90. if (strcmp(word1,set1) != 0)
  91. sscanf(set1, "%s %[^$]", word1, set1);
  92. else
  93. flag1 = 1;
  94. }
  95. else if (flag1 || (!flag2 && (strcmp(word1,word2) > 0))) // all cases to write word2 first
  96. {
  97. if (strcmp(last,word2) != 0)
  98. {
  99. strcat(result,word2);
  100. strcat(result," ");
  101. strcpy(last,word2);
  102. }
  103. if (strcmp(word2,set2) != 0)
  104. sscanf(set2, "%s %[^$]", word2, set2);
  105. else
  106. flag2 = 1;
  107. }
  108. else if (!flag1 && !flag2 && !strcmp(word1,word2)) //if word1 and word2 are same
  109. {
  110. if (strcmp(last,word2) != 0) // confirm that the words are not the last word that be write
  111. {
  112. strcat(result,word2);
  113. strcat(result," ");
  114. strcpy(last,word2);
  115. }
  116. if (strcmp(word2,set2) != 0)
  117. sscanf(set2, "%s %[^$]", word2, set2);
  118. else
  119. flag2 = 1;
  120. if (strcmp(word1,set1) !=0)
  121. sscanf(set1, "%s %[^$]", word1, set1);
  122. else
  123. flag1 = 1;
  124. }
  125. myMergeSets(set1,set2,word1,word2,result,last,flag1,flag2);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement