Advertisement
yarin0600

Untitled

Nov 20th, 2023
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *concatWords(char *w1, char *w2, char *w3);
  6. void initWords(char *w1, char *w2, char *w3);
  7. void initWord(char **w, const char *wordNumber);
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.    char words1[10] = {'\0'};
  12.    char words2[10] = {'\0'};
  13.    char words3[10] = {'\0'};
  14.    initWords(words1, words2, words3);
  15.    return 0;
  16. }
  17.  
  18. char *concatWords(char *w1, char *w2, char *w3)
  19. {
  20.    int i;
  21.    char *w4;
  22.    int totalSize = strlen(w1) + strlen(w2) + strlen(w3) + 1;
  23.    w4 = (char *)malloc(totalSize);
  24.  
  25.    if (!w4)
  26.    {
  27.       printf("Memory allocation failed, try again!\n");
  28.       return NULL;
  29.    }
  30.  
  31.    strcpy(w4, w1);
  32.    strcat(w4, w2);
  33.    strcat(w4, w3);
  34.  
  35.    printf("w4 = %s\n", w4);
  36.    return w4;
  37. }
  38.  
  39. void initWords(char *w1, char *w2, char *w3)
  40. {
  41.    initWord(&w1, "first");
  42.    initWord(&w2, "second");
  43.    initWord(&w3, "third");
  44.  
  45.    char *result = concatWords(w1, w2, w3);
  46.    if (!result)
  47.       return;
  48.  
  49.    printf("result = %s\n", result);
  50.    free(result);
  51. }
  52.  
  53. void initWord(char **w, const char *wordNumber)
  54. {
  55.    printf("Please insert the %s word that it's max length is 10 letters:\n", wordNumber);
  56.    scanf("%10s", *w);
  57.    printf("You chose to insert the word: %s", *w);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement