String Sorting Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

String Sorting Help

 
0
  #1
Dec 10th, 2007
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void main ()
  5. {
  6. char *tokenString, *c, string[80];
  7.  
  8. cout << "Input a string: ";
  9. cin.getline(string, 80);
  10. cout << "The entered string is: " << string << endl;
  11. c = string;
  12. while (*c)
  13. {
  14. if ((*c >= 'a') && (*c <= 'z'))
  15. *c = (*c - 'a') + 'A';
  16. ++c;
  17. }
  18. cout << "The string in all upper case letters is: " << string << endl;
  19.  
  20. tokenString = string;
  21. char *tokenPtr, *nullPtr= NULL;
  22.  
  23. cout << "The string to be tokenized is:\n" << tokenString
  24. << "\n\nThe tokens are:\n";
  25.  
  26. tokenPtr = strtok( tokenString, " " );
  27.  
  28. while ( tokenPtr != NULL ) {
  29. cout << tokenPtr << '\n';
  30. tokenPtr = strtok(nullPtr, " " );
  31. }
  32. }

1) i am trying to get user to input a list of words and then i need to:
* sort them in alpha orders
* and display how many time each word is present in the string. ignore case. for ex: bob == BOB
2) my current code can takes a string input and convert all the words in the string into upper case and tokenized them into parts.
3) i need help on how to compare each of the tokenized words and see if they are equal.
4) samples of ideal inputs and outputs i need my code to do

input
bill bill joe jack dan bob BILL BOB

output
BILL BOB DAN JACK JOE

BILL 3
BOB 2
DAN 1
JACK 1
JOE 1


THANKS
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String Sorting Help

 
0
  #2
Dec 10th, 2007
Here is the standard way to convert a string to upper case
  1. while (*c)
  2. {
  3. *c = toppper(*c);
  4. ++c;
  5. }

After tokenizing the string you need to put them in an array so that you can search the array for a string.
Last edited by Ancient Dragon; Dec 10th, 2007 at 4:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Sorting Help

 
0
  #3
Dec 10th, 2007
TO A.D.


How do i compare the strings to check if any words are the same?

also how should put the tokenized string into a array, a loop?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String Sorting Help

 
0
  #4
Dec 10th, 2007
>>How do i compare the strings to check if any words are the same?
strcmp()

>>also how should put the tokenized string into a array, a loop?
  1. char *array[255] = {0}; // room for 255 strings
  2. int i = 0;
  3. ...
  4. array[i] = tokenPtr;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Sorting Help

 
0
  #5
Dec 12th, 2007
To A.D.:

One more question.

How do i use qsort to sort the tokenized string into alpha order??

thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String Sorting Help

 
0
  #6
Dec 12th, 2007
qsort. See the example in that post.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC