944,192 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1628
  • C RSS
Nov 6th, 2009
0

alphabet sorting for strings

Expand Post »
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
thebluestar is offline Offline
55 posts
since Apr 2009
Nov 6th, 2009
-7
Re: alphabet sorting for strings
>> 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; Nov 6th, 2009 at 10:28 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: classes
Next Thread in C Forum Timeline: huffman code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC