?
Solved

convert 'w' to uppercase

Posted on 2011-04-26
11
Medium Priority
?
302 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

0
Comment
Question by:pgmerLA
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35468773
'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

0
 

Author Comment

by:pgmerLA
ID: 35471317
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35471340
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:pgmerLA
ID: 35471463
nope, it's not supported.
Is there any other way?
sorry to bother you so much, it's gonna be graded.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35471500
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1400 total points
ID: 35471527
Wait, ther would be one way:
#include<iostream>

using namespace std;

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

return 0;}

Open in new window

0
 

Author Comment

by:pgmerLA
ID: 35471544
BINGO!

you rock!!!
0
 
LVL 86

Expert Comment

by:jkr
ID: 35472119
Thanks ;o)
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35474149
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
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 600 total points
ID: 35474170
if you were not allowed to change

char *Days[3]={"Monday","wednesday","Friday"};

Open in new window

you could do

char *Days[3]={"Monday","wednesday","Friday"};
Days[1] = "Wednesday";
cout<<Days[1];

Open in new window



that would exchange the 'wrong' literal by the 'right' one.

Sara
0
 
LVL 11

Expert Comment

by:cup
ID: 35481260
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 Á.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question