| | |
sorting
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
first intialize one string such as str[12]
take two variable of integer type such as i,j
take one temporary variable of char type such as temp
take two variable of integer type such as i,j
take one temporary variable of char type such as temp
c Syntax (Toggle Plain Text)
for(i=0;str[i]!='\0';i++) /*loop will be continue untill the it not found the null charater from str string*/ { for(j=1;str[j]!='\0';j++) { if(str[j]>str[i]) /*chack whether the next charater is smaller than the cuurent charater */ { temp=str[j]; str[j]=str[i]; str[i]=temp; } } } for(i=0;str[i]!='\0';i++) /*print the string */ { printf("%c",str[i]); } }
•
•
Join Date: May 2008
Posts: 561
Reputation:
Solved Threads: 90
@nitu_thakkar
That appears to be a sort for the characters in a string, to put the highest character in the string as the first character.
@taichou
The above sort could be adapted to sort strings in a list.
Where do the words come from?
How do we know how many there are (or will be)?
Once we get them in dictionary order, do we just output the list?
That appears to be a sort for the characters in a string, to put the highest character in the string as the first character.
@taichou
The above sort could be adapted to sort strings in a list.
Where do the words come from?
How do we know how many there are (or will be)?
Once we get them in dictionary order, do we just output the list?
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
i agree with death_oclock and what i type here is actually just more detailed explanation of his suggestion
problem is that if you use standard input (keyboard), you will have to make dynamic array, or to predetermine number of maximum words.
bubble sort is pretty good option since it's simple and easy to use, and you will input form keyboard which means there wont be too many entries.
if you know how to use more complex structures as dynamic arrays, then all you need to do is make one such array. dynamic array works in way that expands with each entry and you will know the length of your array. strcmp or strcmpl (same as previous but capital letters are considered the same as lower case letters) are function that compares two strings. strcmp (string1, string2) evaluates (equals) to 0 if two string are identical, evaluates less than 0 if first string is "lower" (alphabetically comes before second one), and evaluates more than 0 if first string is "higher" (goes after the second).
everything said here expects that you know how to work bubble sort, strings, and dynamic array thing is just optional. you can say that there will be no more than 100 words and you will spare yourself from dynamic arrays.
problem is that if you use standard input (keyboard), you will have to make dynamic array, or to predetermine number of maximum words.
bubble sort is pretty good option since it's simple and easy to use, and you will input form keyboard which means there wont be too many entries.
if you know how to use more complex structures as dynamic arrays, then all you need to do is make one such array. dynamic array works in way that expands with each entry and you will know the length of your array. strcmp or strcmpl (same as previous but capital letters are considered the same as lower case letters) are function that compares two strings. strcmp (string1, string2) evaluates (equals) to 0 if two string are identical, evaluates less than 0 if first string is "lower" (alphabetically comes before second one), and evaluates more than 0 if first string is "higher" (goes after the second).
everything said here expects that you know how to work bubble sort, strings, and dynamic array thing is just optional. you can say that there will be no more than 100 words and you will spare yourself from dynamic arrays.
Last edited by Alibeg; Jan 14th, 2009 at 3:46 pm.
•
•
Join Date: Nov 2008
Posts: 90
Reputation:
Solved Threads: 8
•
•
•
•
how can i arranged the input words alphabetically, like in the dictionary..please help me....please!!!!
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<string.h> void main() { clrscr(); char a[6][7]; int i,j,p; char c[10]; printf("enter the name:"); for(i=0;i<6;i++) scanf("%s",&a[i]); printf("the names entered are:"); for(i=0;i<6;i++) { printf("%s",a[i]); printf("\n"); } for(i=0;i<6;i++) { for(j=i;j<6;j++) { p=strcmp(a[i],a[j+1]); if(p>0) { strcpy(c,a[i]); strcpy(a[i],a[j+1]); strcpy(a[j+1],c); } } } printf("After sorting:\n"); for(i=0;i<6;i++) printf("%s\n",a[i]); getch(); }
Your indentation could use some more noticeable spaces.
#include<conio.h> is not a standard header file.clrscr(); and getch(); are not portable C standard functions.void main() should always return an int, except when there's not host operating system.printf("enter the name:"); you ask for a name, but expect six to be entered via a loop.scanf("%s",&a[i]); a[i] is a pointer already. What if the input is more than seven which is the limit of char a[i][7]? scanf() should always be limited on reading and checked at return for failure.strcmp(a[i],a[j+1]); strcmp(a[5], a[5+1]); What's in a[6]? Oh, there's not an a[6]. Same it is applicable for strcpy() Last edited by Aia; Jan 18th, 2009 at 2:01 pm.
•
•
Join Date: Nov 2008
Posts: 90
Reputation:
Solved Threads: 8
![]() |
Similar Threads
- Need Help for identify the Sorting Action (VB.NET)
- sorting stl::map (C++)
- counting comparisons when sorting (C++)
- sorting an array of string (C)
- help by sorting a simply linked list (C)
- sorting file (C)
- sorting 2d arrays (C)
- re: Sorting Algorithms (C++)
Other Threads in the C Forum
- Previous Thread: bitwise shift opratore....
- Next Thread: how to learn c
| Thread Tools | Search this Thread |
* ansi append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






