We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,924 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

converting lower case words to upper case

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
1 Day
Discussion Span
7 Months Ago
Last Updated
12
Views
poloblue
Junior Poster
104 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

Yusuke00
Newbie Poster
7 posts since Sep 2012
Reputation Points: -5
Solved Threads: 0
Skill Endorsements: 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.

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18
Solved Threads: 36
Skill Endorsements: 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 . . .
poloblue
Junior Poster
104 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

poloblue
Junior Poster
104 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

Of course. Read my previous post.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

poloblue
Junior Poster
104 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

poloblue
Junior Poster
104 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1483 seconds using 2.79MB