Computer Science, asked by surendar2823, 5 months ago

Two strings S1 and S2 can be concatenated to form string S3. If certain characters of the first part of S2 matches with those characters in the last part of S1, they can be merged to occur only once.
Given S1 and S2 as the input, the program must print the minimum possible length of S3.

Input Format:
The first line contains S1.
The second line contains S2.

Output Format:
The first line contains the integer value representing the minimum possible length of S3.

Boundary Conditions:
1 <= Length of S1 and S2 <= 1000

Example Input/Output 1:
Input:
abcdef
efghij

Output:
10

I need to write the code in C​

Answers

Answered by deepakdiwaker310
2

Answer:

Bro The answer will be too long

Explanation:

Answered by qwstoke
0

C Code:

#include <stdio.h>

#include <string.h>

int main() {

   char s1[1001], s2[1001];

   scanf("%s %s", s1, s2);

   // Find the longest common suffix of s1 and prefix of s2

   char common[1001] = "";

   int i, j;

   for (i = strlen(s1)-1, j = 0; i >= 0 && j < strlen(s2); i--, j++) {

       if (s1[i] == s2[j]) {

           common[strlen(common)+1] = '\0';

           common[strlen(common)] = s1[i];

       }

       else {

           break;

       }

   }

   // Concatenate s1 and s2 (minus the common suffix/prefix)

   char s3[2001] = "";

   strcat(s3, s1);

   strcat(s3, s2+strlen(common));

   printf("%d\n", strlen(s3));

   return 0;

}


Note that we use strlen() to find the length of the strings, and strcat() to concatenate the strings. We also use a for loop to compare the characters from the end of s1 and the beginning of s2 to find the longest common suffix/prefix, and then use the + operator to concatenate s2 minus the common suffix/prefix to s1.


#SPJ3

Similar questions