#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char *q) {
char temp, *p;
int i;
while(*q) {
p=q;
i=1;
while(*p) {
if(*p > *(p+i)) {
temp=*(p+i);
*(p+i)=*p;
*p=temp;
}
i++;
p++;
}
q++;
}
}
int main() {
char str[100]="bonjour";
function(str);
printf("%s", str);
return 0;
}
I've been struggling with this code for a while, and I don't know where I am mistaken. I wanted to print a given string in alphabetic order. My program is supposed to swap the previous character with the next one if the the previous character is greater than the next one, but it didn't work at all.
Would you please tell me where did I make a mistake?