I have got an assignment to get input of five names and then sort them alphabetically and print them. Seems too simple?? Even I thought at first. But I have got limits, I cant use multidimensional arrays or any functions in the program. But I can use string functions like strcmp, strcpy, etc. I tried it myself but could not do it. Can anyone please help me??
The code is : #include<stdio.h> #include<string.h> void main() { int i,j; char names[5][10],swap[10]; for(i=0;i<5;i++) { printf("\n Enter the name %d (less than 10 chara): ",i+1); gets(names); } printf("\n"); /*for(i=0;i<5;i++) { printf("\n name %d : ",i+1); puts(names); } */ for(i=0;i<4;i++) { for(j=i+1;j<5;j++) { if(strcmp(names,names[j])>0) { strcpy(swap,names); strcpy(names,names[j]); strcpy(names[j],swap); } } } printf(" Sorted Array \n"); for(i=0;i<5;i++) { printf("\n name %d : ",i+1); puts(names); } } just paste it in a text editor and name it as .c file.
i wont help u how it works... find out yourself and tell your teacher... you should learn something by yourself
@it career's: I didn't ask it without trying it. I could not do it so I just asked for help. @fullydistributed thanks a lot for the code, it worked!! @gramyla: Yes the code worked!!