alphabet sorting for strings

Thread Solved

Join Date: Apr 2009
Posts: 28
Reputation: thebluestar is an unknown quantity at this point 
Solved Threads: 0
thebluestar thebluestar is offline Offline
Light Poster

alphabet sorting for strings

 
0
  #1
21 Days Ago
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4.  
  5. int n;
  6. char (*p)[40],*temp;
  7. void Input();
  8. void ABCsorting();
  9.  
  10. void Input()
  11. {
  12. printf("How many persons u want to input: ");
  13. scanf("%d",&n);
  14. p=(char*)malloc(n*40*sizeof(char));
  15. temp=(char*)malloc(n*sizeof(char));
  16. int i;
  17. for(i=0; i<n ; i++)
  18. {
  19. printf("String %d: ",i+1);
  20. fflush(stdin);
  21. gets(p[i]);
  22. }
  23. printf("All strings before sorting: \n");
  24. /*for(i=0; i<n ; i++)
  25.   {
  26.   printf(" %s\n",*(p+i) );
  27.   } */
  28. for(i=0; i<n ; i++)
  29. printf(" %s |",p[i]);
  30. }
  31.  
  32. void ABCsorting()
  33. {
  34. int i,j;
  35. for(i=0; i<n-1 ;i++)
  36. for(j=i+1; j<n; j++)
  37. {
  38. if( strcmp((p+i),(p+j)) > 0 )
  39. {
  40. temp = (p+i);
  41. (p+i) = (p+j);
  42. (p+j) = temp;
  43. }
  44. /* if( strcmp(p[i], p[j]) > 0)
  45.   {
  46.   temp=p[i];
  47.   p[i]= p[j];
  48.   p[j]=temp;
  49.   }*/
  50. }
  51. printf("\nAfter Sorting: \n");
  52. for(i=0; i<n ;i++)
  53. printf(" %s |",p[i]);
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59. Input();
  60. ABCsorting();
  61. getch();
  62. return 0;
  63. }
Please help me find the mistake in my above source code
Thanks a lot!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
21 Days Ago
>> line 20: fflush(stdin);
fflush() is only guaranteed to work with output streams, not input streams.

>> line 21: gets(p[i])

Two problems:
  1. never ever use gets() because it may write beyone the bounds of the array, causing your program to crash. Use fgets() instead.
  2. Variable p is an array of 40 pointers. You have to allocate memory for those pointers before they can be used. Suggest you code it something like this: char p[40][80]; , which is an array of 40 character arrays, and each array can hold up to 80 characters.

>>line 38: if( strcmp((p+i),(p+j)) > 0 )
Delete that and uncomment the algorithm on line 44.
Last edited by Ancient Dragon; 21 Days Ago at 10:28 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC