#include <stdio.h> #include <string.h> void mergeStr(char *a, char *b, char *c); int main() { char a[80],b[80]; char c[80]; printf("Enter the first string: \n"); gets(a); printf("Enter the second string: \n"); gets(b); mergeStr(a,b,c); printf("mergeStr(): "); puts(c); return 0; } void mergeStr(char *a, char *b, char *c) { int size; int i ; int j=0 ; // again, forgot to initialize j char ch; char temp[80] ; /* Merge string a and string b together, then sort them alphabetically */ c = strcat(a,b) ; size = strlen(c) ; for (ch = 'A' ; ch <= 'z' ; ch++ ) { // iterates from A-Z and a-z for (i=0 ; i<size ; i++) { // which for loop comes first is important, in this case since we want duplicates we should allow i to iterate everything for every case of ch if (c[i] == ch){ temp[j] = c[i] ; j++ ; } } } for (i=0 ; i<size ; i++) { c[i] = temp[i] ; // assign sorted string back to c c[size] = '\0' ; } // puts(c) <- when puts() is run here, desired output is given } In this program, the function takes char a , concatenates it with char b, which is assigned to c.
char c is then sorted and printed out in the main function by puts(c).
For example,
Enter the first string: afkm Enter the second string: bbbggg abbbfgggkm mergeStr(): This is the output i get when puts(c) is run from within void mergeStr() function.
However, puts(c) from int main() does not print anything.
gets(), it's a deprecated old dangerous function.