0

Good Morning,

I'm a little difficulty in writing and implementing a function that read a list of words of variable lengths and convert all letters to upper case. Ask the user to enter the size of the list. I shall use an array as pointers.

So far, I have this, but it's not working. Can anyone help me out.

char toUpper(char * chPtrs[5], char tmpStr[1000], char word)                            
{
    for (int j=0; j<5; j++)
    {
        cout<<"enter a word => ";                     
        cin>>tmpStr;                                    

        chPtrs[j] = new char[strlen(tmpStr)];           
        strcpy(chPtrs[j], tmpStr);  

        static char lower[] = tmpStr;
        for (int i = 0; lower[i] != '0'; i++)
        {
            if (word >= 'a' && word <= 'Z')
                cout << "The word converted to upper case is:"<< static_cast<char> ( word + 'A' - 'a') <<endl;
        }

    }
4
Contributors
10
Replies
12
Views
4 Years
Discussion Span
Last Post by WaltP
0

Hello. I'm working a bit different but i'll present you an valid algorithm

void up()
{
    char word;
        for(int i=0;i<=4;i++)
        {
            cin>>word;
            for(int j=0;j<strlen(word);j++)
                if(word[j]>='a'&&word[j]<='z')
                    cout<<word[j]-32;
                else
                    cout<<word[j];
        }
}

Hope it helps.

0

cout<<"enter a word => ";
cin>>tmpStr;
chPtrs[j] = new char[strlen(tmpStr)];
strcpy(chPtrs[j], tmpStr);

You don't need this. You also can input a whole string using fgets() or getline() functions.

After this, use a loop to traverse the string. Check if the character that is in consideration is lower or not using islower(). It is in cctype header file.

Edited by np complete

0

Hi,

I implemented this

void upperCase (char * chPtrs[5], char tmpStr[1000])
{
    for ( int j = 0; j<5; j++)
    {
        cout << "Enter a word => ";
        cin>>tmpStr;

        chPtrs[j] = new char[strlen(tmpStr)];
        strcpy(chPtrs[j], tmpStr);

    }
    for (int  j = 0; j<5; j++)
        cout <<chPtrs[j]<<endl;
    for (int  j = 0; j<strlen(tmpStr); j++)
        if (tmpStr[j] >= 'a' && tmpStr[j] <= 'z')
            cout <<tmpStr[j] - 32;
        else
            cout <<tmpStr[j];
}

This is the output that I'm getting.

befor calling reverse2( )...
 the string is Computerscience
After calling reverse2 function the string is:ecneicsretupmoc
Enter a word => cat
Enter a word => car
Enter a word => lion
Enter a word => tiger
Enter a word => cougar
cat
car
lion
tiger
cougar
677985716582Press any key to continue . . .
0

for (int j = 0; j<strlen(tmpStr); j++)
Why are you going through tmpStr? Isn't that simply the string you used on input? It has no bearing on your word list once input is done.

0

I have just modified the code:

void upperCase (char * chPtrs[5], char tmpStr[1000])
{
    for ( int j = 0; j<5; j++)
    {
        cout << "Enter a word => ";
        cin>>tmpStr;

        chPtrs[j] = new char[strlen(tmpStr)];
        strcpy(chPtrs[j], tmpStr);

    }
    for (int  j = 0; j<5; j++)
        cout <<chPtrs[j]<<endl;

    for (int  j = 0; j<strlen(tmpStr); j++)
        cout << (char)toupper(tmpStr[j]); 
}

But it's only converting the last word of the list to Uppercase. What should I do so it converts all the words on the list to Uppercases.

Thank you

0

WaltP,

I already remove the for (int j = 0; j<strlen(tmpStr); j++) and it's still only converting the last word to uppercase from the list

0

Then you still did something wrong. Since we can't see what has changed, we can't offer suggestions.

0

WaltP,

I modified the code like this:

void upperCase (char * chPtrs[5], char tmpStr[1000])
{
    for ( int j = 0; j<5; j++)
    {
        cout << "Enter a word => ";
        cin>>tmpStr;

        chPtrs[j] = new char[strlen(tmpStr)];
        strcpy(chPtrs[j], tmpStr);
    }

        for (int  j = 0; j<5; j++)
        {
            cout <<chPtrs[j]<<endl;
        }

        for (int  j = 0; j<strlen(*chPtrs); j++)
            {
                cout << (char)toupper(*(chPtrs[0]+j))<<" ";
                cout << (char)toupper(*(chPtrs[1]+j))<<" ";
                cout << (char)toupper(*(chPtrs[2]+j))<<" ";
                cout << (char)toupper(*(chPtrs[3]+j))<<" ";
                cout << (char)toupper(*(chPtrs[4]+j))<<endl;
            }

}

but it's cutting the words of. Like lion to lio, tiger to tig, bear to bea.

Thanks

0

chPtrs[j] = new char[strlen(tmpStr)]; -- you forgot the extra character for the trailing \0

for (int j = 0; j<strlen(*chPtrs); j++) -- what is the value of strlen(*chPtrs)? Do you really want the string lenght of a pointer? Wouldn't the length of a string be more useful?

And in that loop, why are you outputting exactly 5 characters? LION only has 4 characters, BAT has 3, MONKEY has 6. I'd suggest another loop, using strlen() of the string you are outputting.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.