We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

convert 'w' to uppercase

pgmerLA
pgmerLA asked
on
Medium Priority
310 Views
Last Modified: 2012-05-11
Hi, how can I convert wednesday to Wednesday?
#include<iostream>

using namespace std;

int main()
{
	char *Days[3]={"Monday","wednesday","Friday"};
strupr(Days[1]);
cout<<Days[1];

return 0;}

Open in new window

Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
'strupr()' converts the whole string, which is not what you want. Furthermore, the way you are using string literals they are placed in a 'const' memory segment which does not allow any modifications. Better try
#include<iostream>
#include <ctype.h>

using namespace std;

#define MAX_DAYNAME 16

int main()
{
	char Days[3][MAX_DAYNAME]={"Monday","wednesday","Friday"};
Days[1][0] = toupper(Days[1][0]);
cout<<Days[1];

return 0;}

Open in new window

Author

Commented:
the way I presented it is the way the teacher wants it: char *Days[3]=....
I tried every variation to no avail. I don't know what else to do.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Well, if your compiler supports that, you can of course
#include<iostream>

using namespace std;

int main()
{
	char *Days[3]={"Monday","wednesday","Friday"};
*Days[1]=toupper(*Days[1]);
cout<<Days[1];

return 0;}

Open in new window

Author

Commented:
nope, it's not supported.
Is there any other way?
sorry to bother you so much, it's gonna be graded.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Well, I pretty much assume that your instructor is aware that you cannot change an array of strig literals that way and expects you to change the setup in a way it works... And no, there's not much room for an alternative.
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
BINGO!

you rock!!!
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Thanks ;o)
CERTIFIED EXPERT
Top Expert 2016

Commented:
the last code doesn't change the Days[1] but does the change only on a copied temporary.

you might try

char Days[3][10]={"Monday","wednesday","Friday"};
Days[1][0]=toupper(Days[1][0]);
cout<<Days[1];

Open in new window


the difference is that with your code the Days array contains three pointers to literals where you are not allowed to update them cause they were read-only. in the code above you have char arrays of 10 characters which only were initialized with the literal strings.

Sara
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
cup

Commented:
Note that there is also an _toupper on some machines.  Not all toupper routines work correctly so it is worth checking in a simple noddy program.  Some _toupper/toupper routines will change uppercase back to lowercase.  Also they don't work on anything outside the ASCII 127 range.  For instance, it won't change á to Á.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.