DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   String Sorting Help (http://www.daniweb.com/forums/thread100545.html)

nicz888 Dec 10th, 2007 3:47 pm
String Sorting Help
 
#include <iostream>
using namespace std;

void main ()
{
    char *tokenString, *c, string[80];

    cout << "Input a string: ";
    cin.getline(string, 80);
    cout << "The entered string is: " << string << endl;
    c = string;
    while (*c)
    {
        if ((*c >= 'a') && (*c <= 'z'))
            *c = (*c - 'a') + 'A';
        ++c;
    }
    cout << "The string in all upper case letters is: " << string << endl;

        tokenString = string;
        char *tokenPtr, *nullPtr= NULL;

  cout << "The string to be tokenized is:\n" << tokenString
        << "\n\nThe tokens are:\n";

  tokenPtr = strtok( tokenString, " " );

  while ( tokenPtr != NULL ) {
      cout << tokenPtr << '\n';
      tokenPtr = strtok(nullPtr, " " );
  }
}

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

Ancient Dragon Dec 10th, 2007 4:05 pm
Re: String Sorting Help
 
Here is the standard way to convert a string to upper case
   while (*c)
    {
      *c = toppper(*c);
        ++c;
    }

After tokenizing the string you need to put them in an array so that you can search the array for a string.

nicz888 Dec 10th, 2007 10:19 pm
Re: String Sorting Help
 
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?

Ancient Dragon Dec 10th, 2007 10:25 pm
Re: String Sorting Help
 
>>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?
char *array[255] = {0}; // room for 255 strings
int i = 0;
...
array[i] = tokenPtr;

nicz888 Dec 12th, 2007 3:03 pm
Re: String Sorting Help
 
To A.D.:

One more question.

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

thanks.

Ancient Dragon Dec 12th, 2007 10:08 pm
Re: String Sorting Help
 
qsort. See the example in that post.


All times are GMT -4. The time now is 2:29 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC