String Sort

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1004 Views - Last Post: 24 February 2013 - 09:58 PM Rate Topic: -----

#1 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

String Sort

Posted 24 February 2013 - 01:48 PM

i am making a program to take in integer 'n' then 'n' amount of strings, i then need to sort them by length of size then each string needs to be alphabetically sorted, i can sort the strings out by length but struggling to find a way to run through each character and put them in alphabetical order can anyone shine some light?

my code for sorting out length
 for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if((strlen(strs[i])) > strlen(strs[j]))
            {
                sortTemp = strs[i];
                strs[i] = strs[j];
                strs[j] = sortTemp;
            }
        }
    }



just not sure how to make the next sort for characters.

Is This A Good Question/Topic? 0
  • +

Replies To: String Sort

#2 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 01:50 PM

it's pretty much the same for letters since 'b' > 'a' , 'c'>'b' , etc.
Was This Post Helpful? 0
  • +
  • -

#3 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: String Sort

Posted 24 February 2013 - 01:54 PM

yeah i understand that but if i sort it using

for (int i = 0; i < n; i++)
    {
        for (int j = i+1; j < n; j++)
        {
            if (strs[i] < strs[j])
            {
                sortTemp = strs[i];
                strs[i] = strs[j];
                strs[j] = sortTemp;
            }
        }
    }



it makes no difference as it just goes through each string not each strings character, thats where am stuck
Was This Post Helpful? 0
  • +
  • -

#4 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 01:57 PM

You can treat the vector of strings as a 2d array :)

so string[i][j] would access the letter on the j position from the string on the i position in the vector:)
Was This Post Helpful? 0
  • +
  • -

#5 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: String Sort

Posted 24 February 2013 - 02:05 PM

"Incompatible integer to pointer conversion assigning to 'char *' from 'char'; take the address with &"

it asks for this ^^

 
for (int i = 0; i < n; i++)
    {
        for (int j = i+1; j < n; j++)
        {
            if (strs[i][j] < strs[i][j+1])
            {
                sortTemp = strs[i][j];
                strs[i][j] = strs[i][j+1];
                strs[i][j+1] = sortTemp;
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#6 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 02:06 PM

what type is 'sortTemp' ?
Was This Post Helpful? 0
  • +
  • -

#7 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: String Sort

Posted 24 February 2013 - 02:10 PM

char *sortTemp;

has to be done in heap memory so before all the sorting Ive had to allocate a default memory to each string then reallocate memory to the size of the string an so on, which seems to make the rest of the program tricky, took me while to get my head round it but now Ive hit a wall trying to sort out each alphabetically
Was This Post Helpful? 0
  • +
  • -

#8 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 02:15 PM

you can declare some other variable of type char ...

char* works for the string type since strings are actually vectors of chars, but when you access the string[i][j] element you actually get an item of type char :)

This post has been edited by TwoOfDiamonds: 24 February 2013 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: String Sort

Posted 24 February 2013 - 02:21 PM

sorry am not understanding what to do from what your saying
Was This Post Helpful? 0
  • +
  • -

#10 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 02:27 PM

Well ...

char* is actually a pointer to a char , not an actual char .
So what you are doing by
sortTemp = strs[i][j];
is making the compiler try to convert the strs[i][j] which is a char to an address so it could be asigned to sortTemp .

Basically it's something like char* = 'a' ; and that is not correct for obvious reasons (look up a tutorial on pointers if it isn't obvious .

So what you could do is declare e new variable like
char chSortTemp;


And use it to store the chars you get from strs[i][j] :)
Was This Post Helpful? 0
  • +
  • -

#11 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 02:27 PM

Well ...

char* is actually a pointer to a char , not an actual char .
So what you are doing by
sortTemp = strs[i][j];
is making the compiler try to convert the strs[i][j] which is a char to an address so it could be asigned to sortTemp .

Basically it's something like char* = 'a' ; and that is not correct for obvious reasons (look up a tutorial on pointers if it isn't obvious .

So what you could do is declare e new variable like
char chSortTemp;


And use it to store the chars you get from strs[i][j] :)
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: String Sort

Posted 24 February 2013 - 02:30 PM

If you are sorting each string in an array of strings, then you will need 3 loops (if you are using bubble sort). Create a bubble sort function that will sort a single string. Then loop through every string and call bubble sort.

i.e.
#include <stdio.h>
#include <string.h>

void bubbleSortStr(char *str, int n) {
   //implementation
}

int main() {
   char *strs[10];

   //allocate and initalize strings


   for(int i=0; i<10; i++) {
      bubbleSortStr(strs[i], strlen(strs[i]));
   }
 
   return 0;
}



Quote

You can treat the vector of strings as a 2d array

They are using C, not c++. A vector of c++ strings is not valid.

This post has been edited by jjl: 24 February 2013 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#13 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 02:33 PM

View Postjjl, on 24 February 2013 - 11:30 PM, said:

Quote

You can treat the vector of strings as a 2d array

They are using C strings, not C++ strings, so that is not valid. T


Why not ?

After all , in C it's char[][] , right ? so it's a 2d array of chars ....
Correct me if I'm wrong please :)
Was This Post Helpful? 0
  • +
  • -

#14 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: String Sort

Posted 24 February 2013 - 02:33 PM

View Postjjl, on 24 February 2013 - 11:30 PM, said:

Quote

You can treat the vector of strings as a 2d array

They are using C strings, not C++ strings, so that is not valid. T


Why not ?

After all , in C it's char[][] , right ? so it's a 2d array of chars ....
Correct me if I'm wrong please :)
Was This Post Helpful? 1
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: String Sort

Posted 24 February 2013 - 02:50 PM

Quote

Why not ?

After all , in C it's char[][] , right ? so it's a 2d array of chars ....
Correct me if I'm wrong please

I was referring to the vector of c++ strings, not being valid. Yes, you can treat an array of char pointers as a 2D array.

Sorry for the confusion, I'll clean up my statement.

This post has been edited by jjl: 24 February 2013 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3