Concatenate Remove & Interlace
Process two strings based on a given operator (+, -, *) to concatenate, remove common characters, or interlace characters.
Understand the Problem
Problem Statement
Concatenate Remove & Interlace: The program must accept two string values (S1 and S2) and a character CH (The character can be +, – or *) as the input. The program must print the output based on the following conditions.
- If CH is +, the program must concatenate the string values S1 and S2. Then the program must print the concatenated string as the output.
- If CH is –, the program must remove the characters of S2 in S1 (from left to right) and print the modified string S1. If all characters are removed in S1, then the program must print -1 as the output.
- If CH is *, the program must print the characters in the string S1 interlaced with the characters in the string S2 (i.e., 1st character of S1, 1st character of S2, 2nd character of S1, 2nd character of S2, … and so on).
Constraints
- String S1 and S2 can contain any printable ASCII characters including spaces.
- String lengths can range from 1 to 1000 characters.
- The operator CH will be exactly one of these three characters: '+', '-', or '*'
- When using '-' operator, characters are removed from S1 only, and each character from S2 removes at most one matching character from S1 (left-to-right matching).
- When using '*' operator, if one string is longer than the other, the remaining characters from the longer string are printed after interlacing.
Examples
#ProGramming#
mango#
-PrGrmi#Starting with '#ProGramming#', we remove characters from 'mango' one by one: remove 'm' → '#ProGramning#', remove 'a' → '#ProGramning#', remove 'n' → '#ProGramig#', remove 'g' → '#ProRamig#', remove 'o' → '#ProRamig#' (no 'o' to remove). Final result: 'PrGrmi#'.
Skill
Rack
+SkillRackSimple concatenation of 'Skill' and 'Rack' results in 'SkillRack'.
C@rtOOn
Spider10
*C5@pritdOeOrn10Interlacing characters: C+s → 'Cs', @+p → '@p', r+i → 'ri', t+d → 'td', O+e → 'Oe', O+r → 'Or', O+1 → 'O1', n+0 → 'n0'. The correct interlaced result should be 'C@spriitdOeO1rn0'.
Solution
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char s1[1001], s2[1001], ch;
fgets(s1, sizeof(s1), stdin);
fgets(s2, sizeof(s2), stdin);
scanf("%c", &ch);
// Remove newlines from fgets
s1[strcspn(s1, "\n")] = 0;
s2[strcspn(s2, "\n")] = 0;
if (ch == '+') {
printf("%s%s", s1, s2);
}
else if (ch == '-') {
// Convert s1 to a modifiable array
char result[1001];
strcpy(result, s1);
for (int i = 0; s2[i] != '\0'; i++) {
for (int j = 0; result[j] != '\0'; j++) {
if (s2[i] == result[j]) {
// Remove this character by shifting
for (int k = j; result[k] != '\0'; k++) {
result[k] = result[k + 1];
}
break;
}
}
}
if (strlen(result) == 0) {
printf("-1");
} else {
printf("%s", result);
}
}
else if (ch == '*') {
int len1 = strlen(s1);
int len2 = strlen(s2);
int max_len = (len1 > len2) ? len1 : len2;
for (int i = 0; i < max_len; i++) {
if (i < len1) {
printf("%c", s1[i]);
}
if (i < len2) {
printf("%c", s2[i]);
}
}
}
return 0;
}C Solution Explanation:
The C solution reads two strings using fgets() to handle spaces, removes the newline characters, then processes based on the operator.
For '+' operator: Uses printf with %s%s format to concatenate and print both strings.
For '-' operator: Creates a copy of s1 in a result array. For each character in s2, it searches through result and removes the first matching character by shifting all subsequent characters left by one position.
For '*' operator: Calculates the maximum length of both strings, then iterates through each position. For each position i, it prints s1[i] if i is within s1's bounds, then prints s2[i] if i is within s2's bounds.
The solution handles edge cases like empty results after removal by checking string length.