| | |
HOw to sort the array of strings in c in alphabetical order of words..
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 0
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:
}
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:
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #define max_size 80 int main() { FILE *fp,*fp1; int i; char file1[50],file2[50],buffer[80]; int c; clrscr(); printf("\n Enter File 1:\n"); scanf("%s",file1); printf("\n Enter file 2: "); scanf("%s",file2); fp = fopen(file1,"r"); fp1 = fopen(file2,"w" ); if(fp == NULL) { printf("\n Error in opening the file 1."); } if(fp1==NULL) { printf("\n Error in opening the file 2."); } //fatching line frm file one n storing in to the array Buffer. if (fgets(buffer,max_size,fp)!=NULL); { printf("%s",buffer); /******************************************************From Here I am strugling ... *****************************************************/ /* for (i=0;i<80 ;i++) { if(buffer[i] == " ") { printf("\n harshal"); break; } puts(temp); printf("%s\n",buffer); */ } printf("%s\n",temp); //fputs(buffer,fp1); //c=getc(fp); } fclose(fp); fclose(fp1); getch(); }
Last edited by Narue; Apr 13th, 2009 at 1:09 pm. Reason: added code tags
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.
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.
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:.
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
(4) in line #3, should i use
.
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:.
c Syntax (Toggle Plain Text)
for (j = 0; j < NUMBER_OF_SAMPLES; j++) { for (k = j; k < NUMBER_OF_SAMPLES; k++) { // do that voodoo that you do // ... // ... } }
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.
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.
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.

.
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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: C help with Eclipse
- Next Thread: Deleting a record from a file (appended)
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






