I would like to change the case of the string value.
(lower case letter to upper case and upper case letters to lower case)
I wrote my program as the following. But linux does not know strlwr and strupr functions.
What should I do?

#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=80;

class Reverse
{
char letter[size];
char change[size];

public:

void read()
{
cout<<"\nEnter a letter:";
cin.getline(letter,size);
}

void print()
{
cout<<"\n*******Reverse case version*******";
cout<<endl<<change;
}
void convert();
};

void Reverse::convert()
{
char lower[size];
char upper[size];
//char change[size];
strcpy(lower,letter);
strcpy(upper,letter);
strlwr(lower);
strupr(upper);
int length=strlen(letter);
for(int i=0;i<length;i++)
{
if(letter[i]==lower[i]) {change[i]=upper[i]; }
else change[i]=lower[i];

}
change[i]=NULL;
}
int main()
{
Reverse r1;
r1.read();
r1.convert();
r1.print();
getch();
return 0;
}

Recommended Answers

All 4 Replies

Use tolower() and toupper() in ctype.h
Works on a per-character basis, so you need a loop.

This is a very common question, Salems answer will be perfect for you. But you may want to have a look over some of the topics found here, since they all deal with the same problem and have many explanations of what is going on. It saves us quite a bit of work sometimes if people search what there problem is.
http://www.daniweb.com/search/search.php?q=C%2B%2B+Case+changing

Note: toupper() and tolower() are more reliable that subtract and add '0' to characters because of different character sets!

Chris

For just basic ASCII, XOR 32 works.

Hi Salem,Fneaky-Chris and MosaicFuneral

Thank u so much for answers.
It is a great help for me.


Thanks again!

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.