using strcmp for sorting

Hi guys, I have difficulties on using strcmp.

So the task is to sort the list numerically and alphabetically. I used strtok to divide into tokens. I am getting stuck using strmp.
Can anyone give a clue to use strcmp?

Thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <string.h>

using namespace std;


void fillList (list <char> &myList, const string &strings);
void printList (const list<char> &myList);
int countChar (const list <char> &myList);
void countWord (list <char> &myList);
void divideWord(string &strings);
//void sortingWord (list <char &myList);

int main (int argc, char *argv[])
{
    string strings;
    list <char> myList;
    list <char> cpyList;
    ifstream file (argv[1]);
    if (argc != 2) {
	cout << "usage: " << argv[0] << " <filename>" <<endl;
	return 0;}
    else
	{
		if (!file.is_open())
		{	
		   cout << "Could not open file " << argv[1] << endl;
		   return 1;
		}
		else
		{
		   file.eof();
		   getline(file, strings, '\n');

		}
	}
	fillList(myList, strings);
	countChar(myList);
	countWord(myList);
	divideWord(strings);
	//sortingWord (myList);
}

void fillList (list <char> &myList, const string &strings)
{
    for (int i = 0; i < strings.length(); i++)
    myList.push_back (strings[i]);
}

void printList (const list<char> &myList)
{
   list<char>::const_iterator itr;
   
   for (itr = myList.begin(); itr != myList.end(); itr++ ) {
      cout << *itr;
   }
   cout << '\n';
}

int countChar (const list <char> &myList)
{
   list<char>::const_iterator itr;
   int count = 0;

   for (itr = myList.begin(); itr != myList.end(); itr++ ) {
      count ++;
   }
   cout << "Number of graphical characters are " << count << "\n";
   return count;
}


void divideWord(string &strings)
{
   char *cstr, *pnch;
   cstr = new char [strings.size()+1];
   strcpy (cstr, strings.c_str());
   pnch = strtok (cstr, " ");
   while (pnch!=NULL)
	{
           cout << pnch << endl;
           pnch = strtok(NULL," ");
        }
  delete[] cstr;
}

void countWord (list <char> &myList)
   {
   list<char>::iterator itr;
   int count = 0;

   for (itr = myList.begin(); itr != myList.end(); itr++)
   {
   if ((*itr) == ' ' || (*itr) == ',' || (*itr) == '.' || (*itr) == '-')
   count ++;
   }
   cout << "Number of words are " << count << "\n";
   }

void sortingWord (list <char> &myList, list <char> &cpyList, const string &strings)
{
   list <char> :: const_iterator itr;
   list <int> counter;
   list <int> :: iterator itrc;

   

   for (itr=myList.begin(); itr != myList.end(); itr++)
   {
      // I am stuck in this part! :(
   }
}


If some s1 and s2 were integers, you'd check that s1 < s2. Now, instead you have to check that strcmp(s1, s2) == -1.

Although I don't get what you're doing. list<char> is a list of letters. You either wanted string or list<string>. Also, if you can use string class, you don't need strcmp (in fact, you can't use it at all). Usual operators <, <=, etc work for strings.
hamsterman wrote:
If some s1 and s2 were integers, you'd check that s1 < s2. Now, instead you have to check that strcmp(s1, s2) == -1.
I don't think it is guaranteed that strcmp will return -1 if s1 compares less than s2. Instead check if the returned value is less than 0 strcmp(s1, s2) < 0.
Topic archived. No new replies allowed.