Merging words
You are given two words (not necessarily meaningful words) over the lower case English alphabet. They are to be merged into a single new word in which all the letters (including repetitions) in the given two words occur in increasing order (‘a’ is the least and ‘z’ is the largest) from left to right.
Example : If the two words are “ehllo” and “wrold”, the output is “dehllloorw”.
Explanation : d, e, h , l, o, r, w is the increasing order among the distinct letters. Since all the letters must occur in the output word, the output is “dehllloorw”.
Input : Two words, one on each line.
Output : The merged word as described in the problem statement.
Constraints : 0 < length of each word <= 10.
Hint : Study the sorting algorithms mentioned in the lectures and pick the one which best suits this problem
PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char a[20], b[10], c;
int x[20];
int i,j,l,n,swap;
scanf("%s",a);
scanf("%s",b);
strcat(a,b);
l=strlen(a);
for(i=0; i<l; i++) {
n = a[i];
x[i] = n;
}
for(i=0; i<(l-1); i++) {
for(j=0; j<(l-i-1); j++) {
if(x[j]>x[j+1]) {
swap = x[j];
x[j] = x[j+1];
x[j+1] = swap;
}
}
}
for(i=0; i<l; i++) {
c = x[i];
printf("%c",c);
} }
No comments:
Post a Comment