SHARE
TWEET

Untitled

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