954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sorting an array of string

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 :cool:

matika
Newbie Poster
19 posts since May 2004
Reputation Points: 15
Solved Threads: 0
 

void qsort ( void * base, size_t num, size_t width, int (*fncompare)(const void *, const void *) );
Sort using quicksort algorith.
This function uses an implementation of the quicksort algorithm to sort the num elements of an array pointed by base, each element has the specified width in bytes.
The method used to compare each pair of elements is provided by the caller to this function with fncompare parameter, that is a function called one or more times during the sort process.
Parameters.
-------------
base
Pointer to the first element of the array where the sorting process has to be performed.
num
Number of elements in the array pointed by base.
width
Width of each element in the array.
fncompare
Function that compares two elements. This should be provided by the caller to this function and must follow or be casted to a declaration like:

int fncompare (const void * elem1, const void * elem2 );
The function should receive two parameters (elem1 and elem2) that are pointers to elements, and should return an int value with the result of comparing them:

return value description
<0 *elem1 goes before *elem2
0 *elem1 == *elem2
>0 *elem1 goes after *elem2

Return Value.
(none)

Portability.
Defined in ANSI-C.

Example.

/* qsort example */
#include <stdio.h>
#include <stdlib.h>

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int * pItem;
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
  {
    printf ("%d ",values[n]);
  }
  return 0;
}

Output:
10 20 25 40 90 100
meabed
Junior Poster
Team Colleague
139 posts since May 2004
Reputation Points: 55
Solved Threads: 3
 

:rolleyes: sorry but may be u did not understood my question
i need to sort an array of characters which contains names of students alphabeticly
and i'm asking if there is a function that do this job quickly (sorting the array of chararcters alphabeticly)
thx
matika

matika
Newbie Poster
19 posts since May 2004
Reputation Points: 15
Solved Threads: 0
 

wait... are you trying to list names of students alphabetically or are you trying to change the characters in each students name around into alphabetical order?

kalachylde
Newbie Poster
9 posts since Jun 2004
Reputation Points: 12
Solved Threads: 1
 

hey,I will give you a direction.

You will first have to make a funtion which will compare 2 strings and say which one is greater by comparing the individual chars and returning when it hit's a char which is no equal to the other, like sad sam, (in this case m > d).

Put this into a sort algorithm.

[think this is not much help eh?,see what you can do and you might even suprise yourself.Sometimes just a direction can do wonders.If you still have problems post here,maybe with what you have coded and we will check it out.your will learn more if you do it yourself :cool: :cool: ]

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

i'm trying to list names of students alphabetically

matika
Newbie Poster
19 posts since May 2004
Reputation Points: 15
Solved Threads: 0
 

void arranging()
{
char
temp[30],
name[50][30];

float
temp1,
temp2,
temp3,
temp4,
temp5,
temp6,
grade1[50],
grade2[50],
grade3[50],
grade4[50],
grade5[50],
grade6[50];
int
i=0,
j,
y,
x,
n=0;;

ifstream fin("names.txt");
while(fin)
{
fin.getline(name[i],30);
i++;
}
fin.close();
ifstream finn("grades.txt");
while(finn)
{
finn>>grade1[n];
finn>>grade2[n];
finn>>grade3[n];
finn>>grade4[n];
finn>>grade5[n];
finn>>grade6[n];
n++;
}
finn.close();
for(y=0;y

matika
Newbie Poster
19 posts since May 2004
Reputation Points: 15
Solved Threads: 0
 

C or C++?

The C solution is qsort, and the only difference from what was already shown is the comparison function you write.

In C++ there is a std::sort.

#include <iostream>
#include <vector>
#include <algorithm>
typedef std::vector<std::string>::iterator		 iter;
typedef std::vector<std::string>::const_iterator  citer;
void Show(std::vector<std::string> &alist, const char *label = "")
{
   if ( label )
   {
	  std::cout << label << std::endl;
   }
   for ( citer it = alist.begin(), end = alist.end(); it < end; ++it )
   {
	  std::cout << ' ' << *it << std::endl;
   }
}
int main()
{
   // Make a list.
   std::vector<std::string> mylist;
   mylist.push_back("Phil");
   mylist.push_back("Bob");
   mylist.push_back("Fred");
   // Show the list.
   Show(mylist, "Before:");
   // Sort the list.
   std::sort(mylist.begin(), mylist.end());
   // Show the list.
   Show(mylist, "After:");
   return 0;
}
/* my output
Before:
 Phil
 Bob
 Fred
After:
 Bob
 Fred
 Phil
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

hey,here a simple way to see which of 2 strings is greater :)

int str_alp(char *s1,char *s2)
{
    if(strcmp(s1,s2)==0)return(0);   //if they are equal

        for(int i=0;s1[i]!=0;i++)
        {
              if(s1[i] > s2[i])return(1);
              else if(s1[i] < s2[i])return(2);
        }

    return (2);  //hey if they are not equal and s1 not greater than s2 then s2 is greater
}


See if you can use it

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

>hey,here a simple way to see which of 2 strings is greater

Why not just check to see whether strcmp returns 1 (for greater than)? Or simply return the value returned by strcmp?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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:

dooda man
Newbie Poster
15 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

"radi.h" where can one find it eh?

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 
"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

matika
Newbie Poster
19 posts since May 2004
Reputation Points: 15
Solved Threads: 0
 

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:

Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 16
Solved Threads: 0
 
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 :cool:
#include <stdio.h>
  #include <string.h>

  /* A Quicksort for strings. */
  void quick_string(char items[][10], int count)
  {
    qs_string(items, 0, count-1);
  }

  int qs_string(char items[][10], int left, int right)
  {
    register int i, j;
    char *x;
    char temp[10];

    i = left; j = right;
    x = items[(left+right)/2];
    do {
      while((strcmp(items[i],x) < 0) && (i < right)) i++;
      while((strcmp(items[j],x) > 0) && (j > left)) j--;
      if(i <= j) {
        strcpy(temp, items[i]);
        strcpy(items[i], items[j]);
        strcpy(items[j], temp);
        i++; j--;
     }
    } while(i <= j);

    if(left < j) qs_string(items, left, j);
    if(i < right) qs_string(items, i, right);
  }
int main()
{
  char str[10][10];

    int i,n;
printf("how many string u want to enter:\n");
scanf("%d",&n);
for (i=0;i<n;i++)
{printf("enter %d th string::",i+1);
scanf("%s",str[i]);}
    quick_string(str, n);

    for(i=0; i<n; i++) printf("%s ", str[i]);

    return 0;
  }
sachin_mnnit
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You