Code:
int L;
int main() {
...
L = strlen(S);
...
}
You're using a global variable to pass in strlen(S)?? Something that could have easily been done inside the function? Why? This is terrible style.
Code:
if (n == 0)
s[n]=c[n];
If you know that 'n' is 0 here, you may as well just write s[0] = c[0]; Its clearer to me at least. On a quick glance a reader might get confused.
Code:
memcpy(d,s,strlen(s));
d[strlen(s)]='\0';
...
memcpy(d,str1,strlen(str1));
d[strlen(str1)]='\0';
You do realize those behave exactly the same as
Code:
strcpy(d, s);
strcpy(d, str1);
right? Except the strcat's are going to be easier to read and more efficient (no need to loop through the string to calculate it's length).
Code:
memcpy(str1,d,i);
str1[i]='\0';
Once again, this is equivalent to
Code:
strncpy(str1, d, i);
It's easier to read, use it!
Riiight, pass a character to a function that expects a null-terminated string. THAT's a good idea.
Code:
strncat(str1, &c[n], 1);
Oh wow, much better.
Code:
str2=d+i;
...
strcat(str1,str2);
Really? The WHOLE reason you put an amazingly descriptive 'str2' in there is so you can temporarily store 'd + i' for no reason? If you're going to use variable names like 'str2' you may as well just use 'd + i' in the call to strcat(str1, d + i);
You don't think it's just coincidence that, since you're working with strings, the string handling functions are useful, do you?
Also, the code as you posted it won't even work for at least one reason that I can see. On the first iteration of the loop you are overwriting the null-terminator in 's':
Code:
if(n==0) s[n]=c[n];
On the very next iteration of the loop, you assume you can use strlen(s), which obviously isn't true because you just blew away the null-terminator!
Code:
memcpy(d,s,strlen(s));
And finally, FOR THE LOVE OF GOD LEARN TO INDENT PROPERLY. The forums have a freakin' preview button, why the hell can't anyone figure out how to use it!?
Code:
#include <stdio.h>
#include <string.h>
void insort(int n, char s[], char c[])
{
char d[50], str1[50];
int i;
if (n == 0)
s[0]=c[0];
else {
for (i = n; i >= 0; i--) {
strcpy(d, s);
strncpy(str1, d, 1);
strncat(str1, &c[n], 1);
strcat(str1, d + i);
strcpy(d, str1);
if (c[n] == '\0')
printf("%s\n", d);
}
}
if (c[n] != '\0')
insort(n+1, s, c);
}
int main(void) {
char str[1000] = "";
char S[] = "abcd";
insort(0, str, S);
return 0;
}
Was that so hard? Now go fix it.
(Btw, I still have no idea what this function is supposed to do, your choice of variable names and documentation is exquisite)