Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. void printMenu();
  7. char* getStr();
  8. char* changeFamilyName(char* husbandFullName, char* wifeFullName, int option);
  9. void strcpyByIndex(char* destination, char* source, int start, int size);
  10.  
  11. void main() {
  12.     printf("Enter the husband's full name: ");
  13.     char* husbandFullName = getStr();
  14.     if (!husbandFullName)
  15.         return;
  16.  
  17.     printf("Enter the wife's full name: ");
  18.     char* wifeFullName = getStr();
  19.     if (!wifeFullName)
  20.         return;
  21.  
  22.     printMenu();
  23.     int option;
  24.     printf("Enter your decision: ");
  25.     scanf("%d", &option);
  26.  
  27.     char* newName = changeFamilyName(husbandFullName, wifeFullName, option);
  28.     if (!newName)
  29.         return;
  30.  
  31.     printf("\nBefore the visit:\n%s\n%s\n\n", husbandFullName, wifeFullName);
  32.  
  33.     switch (option) {
  34.     case 1:
  35.         free(husbandFullName);
  36.         husbandFullName = newName;
  37.         break;
  38.     case 2:
  39.         free(husbandFullName);
  40.         husbandFullName = newName;
  41.         break;
  42.     case 3:
  43.         free(wifeFullName);
  44.         wifeFullName = newName;
  45.         break;
  46.     case 4:
  47.         free(wifeFullName);
  48.         wifeFullName = newName;
  49.         break;
  50.     }
  51.  
  52.     printf("After the visit:\n%s\n%s\n\n", husbandFullName, wifeFullName);
  53.  
  54.     // allocated "newName" is also freed by using these
  55.     free(husbandFullName);
  56.     free(wifeFullName);
  57. }
  58.  
  59. void printMenu() {
  60.     printf("Please select one of the folowing options [1-5]: \n");
  61.     printf("1 - Change first partner's last name to second partner's last name.\n");
  62.     printf("2 - Add second partner's last name to first partner's full name.\n");
  63.     printf("3 - Change second partner's last name to first partner's last name.\n");
  64.     printf("4 - Add first partner's last name to second partner's full name.\n");
  65.     printf("5 – Decide Later.\n\n");
  66. }
  67.  
  68. char* getStr() {
  69.     char* str[51];
  70.     gets_s(str, 50);
  71.  
  72.     char* allocatedStr = (char*)malloc((strlen(str) + 1) * sizeof(char));
  73.     if (!allocatedStr) {
  74.         printf("error. couldn't allocate the requsted memory.");
  75.         return NULL;
  76.     }
  77.  
  78.     strcpy(allocatedStr, str);
  79.  
  80.     return allocatedStr;
  81. }
  82.  
  83. char* changeFamilyName(char* husbandFullName, char* wifeFullName, int option) {
  84.     if (!(option <= 5 && option >= 1)) {
  85.         printf("error: option must be 1-5");
  86.         return NULL;
  87.     }
  88.  
  89.     if (!*husbandFullName || !*wifeFullName) { // string are known to be initialized, checking if the first char isn't null
  90.         printf("error: empty string.");
  91.         return NULL;
  92.     }
  93.  
  94.     char* husbandFirstName;
  95.     char* husbandLastName;
  96.     char* wifeFirstName;
  97.     char* wifeLastName;
  98.  
  99.     int husbandFirstNameSize;
  100.     int husbandLastNameSize;
  101.     int wifeFirstNameSize;
  102.     int wifeLastNameSize;
  103.  
  104.     int i;
  105.     for (i = 0; *(husbandFullName + i) != ' '; i++); // husband's first name
  106.     husbandFirstNameSize = i;
  107.     i++;
  108.     husbandFirstName = (char*)malloc(i * sizeof(char));
  109.  
  110.     for (i = 0; *(husbandFullName + i); i++); // husband's last name
  111.     husbandLastNameSize = i;
  112.     husbandLastName = (char*)malloc((i + 1) * sizeof(char));
  113.  
  114.     for (i = 0; *(wifeFullName + i) != ' '; i++); // wife's first name
  115.     wifeFirstNameSize = i;
  116.     i++;
  117.     wifeFirstName = (char*)malloc(i * sizeof(char));
  118.  
  119.     for (i = 0; *(wifeFullName + i); i++); // wife's last name
  120.     wifeLastNameSize = i;
  121.     wifeLastName = (char*)malloc((i + 1) * sizeof(char));
  122.  
  123.     if (!husbandFirstName || !husbandLastName || !wifeFirstName || !wifeLastName) {
  124.         printf("error: couldnt allocate the requested memory.");
  125.         return NULL;
  126.     }
  127.  
  128.     strcpyByIndex(husbandFirstName, husbandFullName, 0, husbandFirstNameSize);
  129.     strcpyByIndex(husbandLastName, husbandFullName, husbandFirstNameSize + 1, husbandLastNameSize);
  130.     strcpyByIndex(wifeFirstName, wifeFullName, 0, wifeFirstNameSize);
  131.     strcpyByIndex(wifeLastName, wifeFullName, wifeFirstNameSize + 1, wifeLastNameSize);
  132.  
  133.     char* newFullName = NULL;
  134.  
  135.     switch (option) {
  136.     case 1:
  137.         newFullName = (char*)malloc((husbandFirstNameSize + wifeLastNameSize + 2) * sizeof(char));
  138.         if (!newFullName) {
  139.             printf("error: couldnt allocate the requested memory.");
  140.             return NULL;
  141.         }
  142.  
  143.         strcpy(newFullName, husbandFirstName);
  144.         strcat(newFullName, " ");
  145.         strcat(newFullName, wifeLastName);
  146.         break;
  147.     case 2:
  148.         newFullName = (char*)malloc((strlen(husbandFullName) + wifeLastNameSize + 2) * sizeof(char));
  149.         if (!newFullName) {
  150.             printf("error: couldnt allocate the requested memory.");
  151.             return NULL;
  152.         }
  153.  
  154.         strcpy(newFullName, husbandFullName);
  155.         strcat(newFullName, " ");
  156.         strcat(newFullName, wifeLastName);
  157.         break;
  158.     case 3:
  159.         newFullName = (char*)malloc((wifeFirstNameSize + husbandLastNameSize + 2) * sizeof(char));
  160.         if (!newFullName) {
  161.             printf("error: couldnt allocate the requested memory.");
  162.             return NULL;
  163.         }
  164.  
  165.         strcpy(newFullName, wifeFirstName);
  166.         strcat(newFullName, " ");
  167.         strcat(newFullName, husbandLastName);
  168.         break;
  169.     case 4:
  170.         newFullName = (char*)malloc((strlen(wifeFullName) + husbandLastNameSize + 2) * sizeof(char));
  171.         if (!newFullName) {
  172.             printf("error: couldnt allocate the requested memory.");
  173.             return NULL;
  174.         }
  175.  
  176.         strcpy(newFullName, wifeFullName);
  177.         strcat(newFullName, " ");
  178.         strcat(newFullName, husbandLastName);
  179.         break;
  180.     case 5:
  181.         printf("\ndecide later chosen\n");
  182.         break;
  183.     default:
  184.         printf("error: option must be between 1 and 5");
  185.         return NULL;
  186.     }
  187.  
  188.     free(husbandFirstName);
  189.     free(husbandLastName);
  190.     free(wifeFirstName);
  191.     free(wifeLastName);
  192.  
  193.     return newFullName;
  194. }
  195.  
  196.  
  197. void strcpyByIndex(char* destination, char* source, int start, int size) {
  198.     int i;
  199.     for (i = start; i < size + start; i++) {
  200.         *(destination + i - start) = *(source + i);
  201.     }
  202.     *(destination + i - start) = 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement