943,524 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 68407
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 11th, 2004
0

Re: sorting an array of string

Just wanted to show how it works inside. It would sure help in understanding what goes on.

btw,what the use if all use something without knowing how it works
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 11th, 2004
-1

Re: sorting an array of string

hi miss matika. strncmp() is the easiet way for comparing strings and it helps in arranging.

for example if you want to arrange accoring to the first letter

strncmp(st1,st2,1) // 1 is the number of letters you are going arrange according to it.

1- the function returns 0 if the letters are the same
2- the function returns -1 if the first letter of st1 smaller than st2
3- the function returns 1 if the first letter of st2 smaller than st1

The question is : Is there is a function which switch the places of two strings with each other.

you can also use this function... balas(); " It is very usefull trust me "
but u must include "radi.h"
thnx
bye :cheesy:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dooda man is offline Offline
15 posts
since May 2004
Jun 12th, 2004
0

Re: sorting an array of string

"radi.h" where can one find it eh?
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 13th, 2004
0

Re: sorting an array of string

Quote originally posted by FireNet ...
"radi.h" where can one find it eh?
it's one of my colleages he likes to make jokes a lot
sorry
there is nothing called radi(); or a header file called "radi.h"
sorry guys agaian :o
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Jun 17th, 2004
0

Re: sorting an array of string

The answer is quite simple:
Just use "<"or">" operators in an "if" instruction like in a normal sorting method for numbers. Be careful though:
if you have:
s1[]="abcdef",s2[]="afsdg"; and you write
if (s1>s2) printf("1");
else printf("2"); it will write 2 as in s2 larger than s1 so s1 has to be first: abcdef, afsdg

Hope it helps! :cheesy:
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Mar 22nd, 2010
-2
Re: sorting an array of string
Click to Expand / Collapse  Quote originally posted by matika ...
is there any function that helps to sort an array of strings alphabeticly
i need to sort an array alphabeticly which contains names of students
thx 4 yr help
matika
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /* A Quicksort for strings. */
  5. void quick_string(char items[][10], int count)
  6. {
  7. qs_string(items, 0, count-1);
  8. }
  9.  
  10. int qs_string(char items[][10], int left, int right)
  11. {
  12. register int i, j;
  13. char *x;
  14. char temp[10];
  15.  
  16. i = left; j = right;
  17. x = items[(left+right)/2];
  18. do {
  19. while((strcmp(items[i],x) < 0) && (i < right)) i++;
  20. while((strcmp(items[j],x) > 0) && (j > left)) j--;
  21. if(i <= j) {
  22. strcpy(temp, items[i]);
  23. strcpy(items[i], items[j]);
  24. strcpy(items[j], temp);
  25. i++; j--;
  26. }
  27. } while(i <= j);
  28.  
  29. if(left < j) qs_string(items, left, j);
  30. if(i < right) qs_string(items, i, right);
  31. }
  32. int main()
  33. {
  34. char str[10][10];
  35.  
  36. int i,n;
  37. printf("how many string u want to enter:\n");
  38. scanf("%d",&n);
  39. for (i=0;i<n;i++)
  40. {printf("enter %d th string::",i+1);
  41. scanf("%s",str[i]);}
  42. quick_string(str, n);
  43.  
  44. for(i=0; i<n; i++) printf("%s ", str[i]);
  45.  
  46. return 0;
  47. }
Last edited by peter_budo; Mar 22nd, 2010 at 4:20 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sachin_mnnit is offline Offline
1 posts
since Mar 2010

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: C output problem
Next Thread in C Forum Timeline: converting from binary to decimal and vice versa





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


Follow us on Twitter


© 2011 DaniWeb® LLC