Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ELAD 0
- void printMenu();
- char* getStr();
- char* changeFamilyName(char* husbandFullName, char* wifeFullName, int option);
- void strcpyByIndex(char* destination, char* source, int start, int size);
- void main() {
- printf("Enter the husband's full name: ");
- char* husbandFullName = getStr();
- if (!husbandFullName)
- return;
- printf("Enter the wife's full name: ");
- char* wifeFullName = getStr();
- if (!wifeFullName)
- return;
- printMenu();
- int option;
- printf("Enter your decision: ");
- scanf("%d", &option);
- char* newName = changeFamilyName(husbandFullName, wifeFullName, option);
- if (!newName)
- return;
- printf("\nBefore the visit:\n%s\n%s\n\n", husbandFullName, wifeFullName);
- switch (option) {
- case 1:
- free(husbandFullName);
- husbandFullName = newName;
- break;
- case 2:
- free(husbandFullName);
- husbandFullName = newName;
- break;
- case 3:
- free(wifeFullName);
- wifeFullName = newName;
- break;
- case 4:
- free(wifeFullName);
- wifeFullName = newName;
- break;
- }
- printf("After the visit:\n%s\n%s\n\n", husbandFullName, wifeFullName);
- // allocated "newName" is also freed by using these
- free(husbandFullName);
- free(wifeFullName);
- }
- void printMenu() {
- printf("Please select one of the folowing options [1-5]: \n");
- printf("1 - Change first partner's last name to second partner's last name.\n");
- printf("2 - Add second partner's last name to first partner's full name.\n");
- printf("3 - Change second partner's last name to first partner's last name.\n");
- printf("4 - Add first partner's last name to second partner's full name.\n");
- printf("5 – Decide Later.\n\n");
- }
- char* getStr() {
- char* str[51];
- gets_s(str, 50);
- char* allocatedStr = (char*)malloc((strlen(str) + 1) * sizeof(char));
- if (!allocatedStr) {
- printf("error. couldn't allocate the requsted memory.");
- return NULL;
- }
- strcpy(allocatedStr, str);
- return allocatedStr;
- }
- char* changeFamilyName(char* husbandFullName, char* wifeFullName, int option) {
- if (!(option <= 5 && option >= 1)) {
- printf("error: option must be 1-5");
- return NULL;
- }
- if (!*husbandFullName || !*wifeFullName) { // string are known to be initialized, checking if the first char isn't null
- printf("error: empty string.");
- return NULL;
- }
- char* husbandFirstName;
- char* husbandLastName;
- char* wifeFirstName;
- char* wifeLastName;
- int husbandFirstNameSize;
- int husbandLastNameSize;
- int wifeFirstNameSize;
- int wifeLastNameSize;
- int i;
- for (i = 0; *(husbandFullName + i) != ' '; i++); // husband's first name
- husbandFirstNameSize = i;
- i++;
- husbandFirstName = (char*)malloc(i * sizeof(char));
- for (i = 0; *(husbandFullName + i); i++); // husband's last name
- husbandLastNameSize = i;
- husbandLastName = (char*)malloc((i + 1) * sizeof(char));
- for (i = 0; *(wifeFullName + i) != ' '; i++); // wife's first name
- wifeFirstNameSize = i;
- i++;
- wifeFirstName = (char*)malloc(i * sizeof(char));
- for (i = 0; *(wifeFullName + i); i++); // wife's last name
- wifeLastNameSize = i;
- wifeLastName = (char*)malloc((i + 1) * sizeof(char));
- if (!husbandFirstName || !husbandLastName || !wifeFirstName || !wifeLastName) {
- printf("error: couldnt allocate the requested memory.");
- return NULL;
- }
- strcpyByIndex(husbandFirstName, husbandFullName, 0, husbandFirstNameSize);
- strcpyByIndex(husbandLastName, husbandFullName, husbandFirstNameSize + 1, husbandLastNameSize);
- strcpyByIndex(wifeFirstName, wifeFullName, 0, wifeFirstNameSize);
- strcpyByIndex(wifeLastName, wifeFullName, wifeFirstNameSize + 1, wifeLastNameSize);
- char* newFullName = NULL;
- switch (option) {
- case 1:
- newFullName = (char*)malloc((husbandFirstNameSize + wifeLastNameSize + 2) * sizeof(char));
- if (!newFullName) {
- printf("error: couldnt allocate the requested memory.");
- return NULL;
- }
- strcpy(newFullName, husbandFirstName);
- strcat(newFullName, " ");
- strcat(newFullName, wifeLastName);
- break;
- case 2:
- newFullName = (char*)malloc((strlen(husbandFullName) + wifeLastNameSize + 2) * sizeof(char));
- if (!newFullName) {
- printf("error: couldnt allocate the requested memory.");
- return NULL;
- }
- strcpy(newFullName, husbandFullName);
- strcat(newFullName, " ");
- strcat(newFullName, wifeLastName);
- break;
- case 3:
- newFullName = (char*)malloc((wifeFirstNameSize + husbandLastNameSize + 2) * sizeof(char));
- if (!newFullName) {
- printf("error: couldnt allocate the requested memory.");
- return NULL;
- }
- strcpy(newFullName, wifeFirstName);
- strcat(newFullName, " ");
- strcat(newFullName, husbandLastName);
- break;
- case 4:
- newFullName = (char*)malloc((strlen(wifeFullName) + husbandLastNameSize + 2) * sizeof(char));
- if (!newFullName) {
- printf("error: couldnt allocate the requested memory.");
- return NULL;
- }
- strcpy(newFullName, wifeFullName);
- strcat(newFullName, " ");
- strcat(newFullName, husbandLastName);
- break;
- case 5:
- printf("\ndecide later chosen\n");
- break;
- default:
- printf("error: option must be between 1 and 5");
- return NULL;
- }
- free(husbandFirstName);
- free(husbandLastName);
- free(wifeFirstName);
- free(wifeLastName);
- return newFullName;
- }
- void strcpyByIndex(char* destination, char* source, int start, int size) {
- int i;
- for (i = start; i < size + start; i++) {
- *(destination + i - start) = *(source + i);
- }
- *(destination + i - start) = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement