HOw to sort the array of strings in c in alphabetical order of words..

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 1
Reputation: harshaldhote is an unknown quantity at this point 
Solved Threads: 0
harshaldhote harshaldhote is offline Offline
Newbie Poster

HOw to sort the array of strings in c in alphabetical order of words..

 
0
  #1
Apr 13th, 2009
hello all,
I have to write the code to read the file line by line and sort its words in alphabetical order n store into another file, i have done with the feathing the line from one file but strugling with the sorting part can anybody suggest how should i move forward..

here is code i have written:
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define max_size 80
  4.  
  5. int main()
  6. {
  7.  
  8. FILE *fp,*fp1;
  9. int i;
  10. char file1[50],file2[50],buffer[80];
  11.  
  12. int c;
  13. clrscr();
  14. printf("\n Enter File 1:\n");
  15. scanf("%s",file1);
  16.  
  17. printf("\n Enter file 2: ");
  18. scanf("%s",file2);
  19.  
  20. fp = fopen(file1,"r");
  21. fp1 = fopen(file2,"w" );
  22.  
  23. if(fp == NULL)
  24. {
  25. printf("\n Error in opening the file 1.");
  26. }
  27. if(fp1==NULL)
  28. {
  29. printf("\n Error in opening the file 2.");
  30. }
  31.  
  32. //fatching line frm file one n storing in to the array Buffer.
  33. if (fgets(buffer,max_size,fp)!=NULL);
  34. {
  35.  
  36. printf("%s",buffer);
  37. /******************************************************From Here I am strugling ...
  38. *****************************************************/
  39.  
  40. /* for (i=0;i<80 ;i++)
  41.  {
  42.   if(buffer[i] == " ")
  43.   {
  44.   printf("\n harshal");
  45.   break;
  46.   }
  47.  puts(temp);
  48.  
  49.  printf("%s\n",buffer);
  50. */
  51. }
  52. printf("%s\n",temp);
  53. //fputs(buffer,fp1);
  54. //c=getc(fp);
  55.  
  56. }
  57.  
  58. fclose(fp);
  59. fclose(fp1);
  60. getch();
  61. }
}
Last edited by Narue; Apr 13th, 2009 at 1:09 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: HOw to sort the array of strings in c in alphabetical order of words..

 
1
  #2
Apr 13th, 2009
Read this article and pick an algorithm to use. To sort strings, the easiest way is to compare with the strcmp function and swap with the strcpy function (both found in the string.h header).

Note that to sort all of the strings, you'll want to keep them all in memory at one time, such as with an array of strings. It's possible to sort without having everything in memory, but it's probably more complicated to do so than you're ready for.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: zaker ali is an unknown quantity at this point 
Solved Threads: 0
zaker ali zaker ali is offline Offline
Newbie Poster

Re: HOw to sort the array of strings in c in alphabetical order of words..

 
0
  #3
Apr 13th, 2009
temp is not declare upside, so puts(temp); function gets error, and for files ,buffer , we have to use gets() function , after printf("buffer\n"); but not %s in printf(" ");
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,632
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 122
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: HOw to sort the array of strings in c in alphabetical order of words..

 
0
  #4
Apr 13th, 2009
^ WTF did you just say?

Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,632
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 122
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: HOw to sort the array of strings in c in alphabetical order of words..

 
0
  #5
Apr 13th, 2009
here's a simple algorithm you can try.

1) read your values into an array. lets say you have ten values, randomly located in array[0] - array[9]
2) decide which end will be lowest value, which will be highest. say array[0] will be low, and array[9] will be high.
3) start with the element in array[0] and compare it to each of the others, 1 - 9
4) if one of the elements is smaller than array[0], swap them. compare each of the remaining elements in turn. once you're done comparing to all nine other elements, array[0] will be the lowest value.
5) next look at array[1] and do the same thing, comparing against elements 2 - 9, swapping if its value is smaller. leave array[0] alone since it's already the smallest. once youre done, array[1] will then have the second-smallest value.
6) then go and get array[2] and compare it to each of the remaining elements, 3 - 9, leaving elements in array[0] and array[1] alone.
7) ....
8) and so on until you're done.

you'll need a nested loop to accomplish this. an example, but not necessarily a completely accurate example, is like such:.

  1. for (j = 0; j < NUMBER_OF_SAMPLES; j++)
  2. {
  3. for (k = j; k < NUMBER_OF_SAMPLES; k++)
  4. {
  5. // do that voodoo that you do
  6. // ...
  7. // ...
  8. }
  9. }

some questions for you to consider:

(1) how will you read all of the file into an array to begin with? hint: you're on the right track with "fgets()"...
(2) this example is for values. can alphabetical strings be treated as a value? how?
(3) in line #1, should i use (NUMBER_OF_SAMPLES - 1) or (NUMBER_OF_SAMPLES + 1) or leave it as is? why?
(4) in line #3, should i use k = (j+1) or k = (j-1) or leave it as is? why?



.
Last edited by jephthah; Apr 13th, 2009 at 7:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: HOw to sort the array of strings in c in alphabetical order of words..

 
0
  #6
Apr 13th, 2009
If possible (this case) better sort an array of pointers to words but not an array of words. In actual fact it's unclear what to do with multiple occurences of the same word in OP assignment. An optimal algorithm (and data structure) depends on this specification. For example, if you want to print text dictionary, better place all scanned words in some kind of (semi) balanced tree (AVL tree is a good candidate) then close file and make this tree traversal - all in one (pass), simple (no need in a special sort step) and fast O(n*log(n)) algorithm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,632
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 122
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: HOw to sort the array of strings in c in alphabetical order of words..

 
0
  #7
Apr 13th, 2009
yeah, that's nice and all, but i think this is just a CSC 101 assignment to learn how to sort strings alphabetically.

i mean your 400-level solution is impressive, but i doubt fast tree algorithms is what a 100-level beginning student needs, when they're still struggling with loops and arrays.




.
Last edited by jephthah; Apr 13th, 2009 at 8:40 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC