sorting using strcmp alphabetically

May 1, 2012 at 7:29am
Hi guys, I am stuck in sorting the string list.

THe output i want is to sort the strings alphabetically. Can someone help me?

1
2
3
4
5
6
7
8
9
10
11
12
void sortingWord (list <string> &myList, string &word)
{
   list <string> :: iterator itr, temp;

   for (itr=myList.begin(); itr != myList.end(); itr++)
   {
         if (strcmp(word.c_str(),(*temp).c_str())>0)
	 {
	   
	 }
   }
}
May 1, 2012 at 7:57am
I think you need two loops, not one. Please check this post:

http://www.cplusplus.com/forum/beginner/69717/

It depends on the sorting algorithm you wanna do. The simplest is the one mentioned in that post.
Last edited on May 1, 2012 at 7:58am
May 1, 2012 at 8:00am
I dont really quite understand the logic behind it. can you please explain to me nad probably give me some pseudocode?

I saw the post and I am still confused since I am new to c++.

Thanks :)
May 1, 2012 at 8:31am
hmmmmmmmmm... You're using iterators in your code and you're saying you're new in C++... this is so confusing...

What do you know in C++? do you have a working compiler?

Do you want to write your own sorting algorithm? or is it OK to use std::sort()?

http://www.cplusplus.com/reference/algorithm/sort/
May 1, 2012 at 9:09am
I mean, I know how to use iterator is to find the value inside the list.

I wanted to use strcmp() to compare the string but what confuse me is,

what I have to compare from the list. I don't really quite get it.

I have to use the strcmp and sort whatever in the .txt file alphabetically.

Do you understand what I mean?
May 1, 2012 at 9:45am
I think for sorting strings algorithm, you can't use strcmp. strcmp finds the value difference between whole words.

What you have to do, is compare characters. Luckily, char in C++ can be considered as integers. So all you have to do is the following:

1- Get the maximum word length
2- Loop over the words and sort by comparing the int value of the first character.
3- Loop over the words that have the same first character, and sort by the second character's int value
4- Loop over the words that have the same first and second characters, and sort by the third character.
...

Keep doing that till you get the whole list sorted.

Remember, you can use operator[] to access the characters in std::string.

Is that good enough for a start?
May 1, 2012 at 10:49am
How do you compare the int value of the first character?

For example the .txt file is "I want to eat chicken"

the output when using the code is "chicken eat I to want"

May 1, 2012 at 11:20am
Simply C-cast it!

1
2
3
4
5
6
7
8
9
10
11
12
13
std::string myStr1,myStr2;

myStr1 = "I wanna eat chicken";
myStr2 = "He wanna eat chicken";

if((int)(myStr1[0]) <= (int)(myStr2[0])) //probably it could work without the (int) casting, try it
{
    std::cout<<"myStr1 goes first"<<std::endl;
}
else
{
    std::cout<<"myStr2 goes first"<<std::endl;
}


Here you can find a table for each char and its int value:

http://en.wikipedia.org/wiki/ASCII

Remember, capital and small characters have different values. Probably you wanna use the functions toupper() and tolower() to convert the char to a unfied form before comparing.

http://www.cplusplus.com/reference/clibrary/cctype/toupper/
http://www.cplusplus.com/reference/clibrary/cctype/tolower/

And notice that those functions take an int as a argument, which is equivalent to char. The only difference is that int could be 4 or 8 bytes, but char is 1 byte. So char is practically a number from 0 to (2^8-1) = 255, which the compiler interprets it as a character.
Last edited on May 1, 2012 at 11:21am
May 1, 2012 at 11:25am
To sort individual words, you have to parse them for spaces, and then sort them, and then combine them again.

To split them, check this out:

http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
May 1, 2012 at 11:31am
> I am stuck in sorting the string list.

Why can't you just use std::sort()?
std::sort( myList.begin(), myList.end() ) ;


> I have to use the strcmp and sort whatever in the .txt file alphabetically.

if you have to use strcmp:

1
2
bool my_cmp( const std::string& a, const std::string& b ) 
{ return std::strcmp( a.c_srt(), b.c_str() ) < 0 ; }


And then: std::sort( myList.begin(), myList.end(), my_cmp ) ;
Topic archived. No new replies allowed.