#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

Recommended Answers

All 5 Replies

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.

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?

>>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;

To A.D.:

One more question.

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

thanks.

qsort. See the example in that post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.