In: Computer Science
C
Write a function that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function should return 1. Otherwise, the function should return 0.
#include <stdio.h> #include <string.h> int func(char* s1, char* s2){ if(s1==NULL || s2==NULL){ return 1; } else{ char tmp[100]; int i,k; //strcpy(tmp, s1); for(i = 0;s1[i]!='\0';i++){ tmp[i] = s1[i]; } tmp[i] = '\0'; //strcat(s1,s2); for(k = 0;s2[k]!='\0';k++){ s1[i++] = s2[k]; } s1[i] = '\0'; //strcpy(s2,tmp); for(i = 0;s1[i]!='\0';i++){ s2[i] = tmp[i]; } s2[i] = '\0'; return 0; } } int main() { char s1[100]; char s2[100]; int res; printf("Enter string 1: "); scanf("%s",&s1); printf("Enter string 2: "); scanf("%s",&s2); res = func(s1,s2); if(res == 0){ printf("s1 = %s\n",s1); printf("s2 = %s\n",s2); } else{ printf("Invalid input\n"); } return 0; }