You are here:

C++/C++ Problem

Advertisement


Question
Hi,
 I have a problem my instructor gave us a program and I have no idea how to start it. The instructor told us he didn't know how to do it so we couldn't ask questions. So I hope you can help me. We have to write a simple job he said it should be three lines of code and a function. We have to print out the Ascii code upper and lower case. So we have to write a function that calls the Ascii code. I hope you can help thank you for your time. I'm using a C++ Compiler.

Answer
I'm not quite sure what you mean by writing a function that calls the ASCII code. ASCII is a character set, not any kind of executable instructions. Perhaps if you gave me sample input and output of the function I could be of more help.

Printing the upper and lower case versions of a character is very simple with the C++ standard functions toupper and tolower:

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
   char c;

   while (cin>>c) {
       cout<< char(toupper(c)) <<'\t'<< char(tolower(c)) <<endl;
   }
}

If you're assuming ASCII then you can do it manually instead:

#include <iostream>

using namespace std;

int main()
{
   char c;

   while (cin>>c) {
       if (c >= 'a' && c <= 'z')
         cout<< char(c + 'A' - 'a') <<'\t'<< c <<endl;
       else if (c >= 'A' && c <= 'Z')
         cout<< c <<'\t'<< char(c + 'a' - 'A') <<endl;
       else
         cout<< c <<'\t'<< c <<endl;
   }
}

-Sandra

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sandra Guth

Expertise

I can help with any questions concerning ISO standard C++, object oriented programming and design as well as algorithms and data structures.

Experience

I have nine years of programming experience with C++.

©2016 About.com. All rights reserved.