Hi! Im doing an introduction to programming course in college and I have only been studying computer programming for the past six weeks.

Our lecturer has given us an assignment to do by thursday which is to write a program that converts all of the uppercase letters to lowercase letters and vice versa!

I would be very grateful to anyone who help me please or tell me where I could get help (as soon as possible), as I really havn't a clue how to do this!

Recommended Answers

All 23 Replies

Em yep I think we are using character arrays, but I've never seen std::string before.

Oh and thanks a million for the link!

I've actually just properly read the assignment and this is what it says;

Lowercase letters are separated from uppercase letters by 32 characters. Thus to convert a lowercase to an uppercase letter, subtract 32 from it. Use this information to write a program that reads characters from the keyboard. Have it convert all lowercase letters to uppercase and all uppercase to lowercase, displaying the result. Make no changes to any other character. Have the program stop when the user enters a full stop ‘.’. At the end of the program display the number of case changes that have taken place.

Do you know if it would be okay to use a program like this for it and just change it around a bit?

#include <iostream>

using namespace std;

int main ()
{
    double f; //here go feet
    double m; //here go metres

    cout << "Enter the length in feet: ";
    cin >> f;
    m = f * 0.3048;

    cout << f << " feet is " << m << "metres\n";

    return 0;
}

Or would that be completely wrong?
Im so confused!

Please use code tags when you post code.

That program has nothing to do with case changing!

You should check the input, one character at a time, and see if its ascii value falls in a specified range (65-91 for upper case, etc). If it does, modify it. Doe the same for the lower case characters.

You'll have to show us you've tried before getting much more help :)

David

Every character in the alphabet is represented by an integer called and ASCII value. Lower-Case (minuscule) characters have higher ASCII values than Upper-Case (majuscule) characters. Relying on this fact, you can use characters directly in comparison statements to determine what actions to perform, if any.

Here is a link to the ASCII Table. I suggest you take a close look at it.

>>Thus to convert a lowercase to an uppercase letter, subtract 32 from it.

Wrong -- that could be disasterous if done dimwittedly. The macro toupper() and tolower() should be used to convert a character from upper to lower, or vice versa because it is part of the C and C++ standards, and is portable across all compilers and operating systems.

#include <iostream>

using namespace std;

int main()
{
    char u; //here go all of the uppercase letters
    char l; //here go all of the lowercase letters

    cout << "Enter all of the uppercase letters: ";
    cin >> u;
    u = l - 32;

    cout << u << " uppercase letters are " << l << "lowercase letters\n";

    return 0;
}

Actually, I tried the above example and all it did was ask me to enter all of the uppercase letters.

Could you maybe advise me on where I'm going wrong please?

okay i just read the posts above my one and i still don't really understand anything!

thanks anyway!

Again, please use code tags.

You have read the value into 'u', and then overriden it by 'l-32'. Worse, 'l' is undefined.

Also, by the way, this is just operating on a single character.

To convert a character from lower to upper case

char c = 'A';
cout << tolower(c);

and to convert from lower to upper, replace tolowe() with toupper(). Subtracting 32 from the character can easily lead to disaster.

#include <iostream>

using namespace std;

int main()
{
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

Okay, so could someone tell me how to get this code to keep asking ya to enter letters, like how to get it to run on a loop or something?

Use a while loop:

cin >> ch;
while(ch != some_exit_value)
{
 // do you conversion
 // cin >> ch;
}

okay thanks!

#include <iostream>

using namespace std;

int main ()

{
  char ch;

  cout<<"Enter a character: ";
  cin >> ch;

  while (ch != .)

  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

I'm sorry i keep asking questions, but is this right or do i have to put it in a different place?

[offtopic]
A friendly piece of advice:
Have you tried to compile and run this? I don't see this compiling successfully (I'll explain below). Always test your code before you post it. If you don't, you can't respond intelligently to other poster's questions and you start to look like you're just making wild guesses instead of actually trying.
[/offtopic]

#include <iostream>

using namespace std;

int main ()

{
  char ch;

  cout<<"Enter a character: ";
  cin >> ch;

  while (ch != .)

  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

I'm sorry i keep asking questions, but is this right or do i have to put it in a different place?

Don't apologize for asking questions, that's why we're all here. The location should be fine, but I see a couple things.

  1. You should put 'single quotes' around your "full-stop" in your loop condition because it is a single char that you are trying to compare a value to.
  2. Add some braces to control the scope of your while loop. As written it should work, but you need to add to it (see the next item).
  3. You need to add another input prompt at the end of your while loop. Otherwise, you will probably get an infinite loop. Have another look at the comments in daviddoria's post.

If you correct your syntax, and add another input statement, it should be okay.

commented: words! +11
#include <iostream>

using namespace std;

int main ()

{
  char ch;
  
cout<<"Enter a character: ";
  cin >> ch;

while (ch != '.');

  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream

 while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

Fbody thanks, but i don't really know where to put the curly braces, i added the single quotes though.
And do i just put a cin >> ch; at the end of the while loop?

Sorry,i dont really know what im doing when it comes to things like this!

Oh and i did try running it and it didnt work, thats why i keep asking questions.
It worked before i put in the while loop, but i just need it to keep saying "Enter a character" until a full stop is entered. But i dont know how to do this properly! :S

>>but i don't really know where to put the curly braces
Read this. Then, get rid of the semi-colon. If you put a semi-colon immediately after a conditional like that, it completely messes up the logic.

>>And do i just put a cin >> ch; at the end of the while loop?
Yep, that's all you really need. I would advise putting an output statement (prompt) with it so that your user knows the program is waiting for input. But it's not absolutely necessary.

>>Oh and i did try running it and it didnt work, thats why i keep asking questions.
I assure you, I know that. My comment was based on the fact that you should have gotten a compiler error flagging your conditional on your while loop, but you didn't mention anything about it. If you knew about the error, you would have tried to correct it. If you couldn't correct it, you'd have asked about the issue. Do you see where this is going? Just make sure you are detailed when describing your issue(s).

You essentially have it, you just need to get these last couple pieces of logical structure in place. Make sure that your if-else if-else section and the second input prompt are all part of your loops "statement block" (you'll know what this is if you read the link I gave you above).

ok. thanks a million :)

Okay so far I have this;

#include <iostream>

using namespace std;

int main ()
    {
  char ch;

while (ch != '.')

  {cout<<"Enter a character: ";
  cin >> ch;

  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;}

  while ( cin.get ( ch ) && ch != '\n' )
  return 0;

}

And it works, but now all I need it to do is to count and display the number of case changes... anyone any ideas?

Oh and thanks everyone for helping me get this far! :)

Hmmm..... I had some trouble seeing the limits of your loop at first, but it looks okay.

On that note, have you been taught anything about formatting? You may want to have a look at this (you can thank WaltP if you see him).

There are 2 common ways of formatting a statement block for easier readability. The primary difference is where you position the opening brace for the block, and neither version is better nor more-appropriate than the other, it's just personal preference.:

while (ch != '.') {  //<---brace position, option 1, plainly visible
{  //<---brace position, option 2, plainly visible
 
  {cout<<"Enter a character: ";  //this brace is hard to see, bad readability
  cin >> ch;
 
  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;}  //this brace is hard to see, bad readability
}  //<---easier to read brace position, plainly visible

>>all I need it to do is to count and display the number of case changes... anyone any ideas
Do you know what an "accumulator" is? You'll need one and you'll have to expand the statement blocks (hint, hint) that make up your if.

Thanks a million Fbody :)

  1. The ASCII lowercase letters are separated from the uppercase letters by 32. Thus, to convert
    a lowercase letter to uppercase, subtract 32 from it. Use this information to write a program
    that reads characters from the keyboard. Have it convert all lowercase letters to uppercase,
    and all uppercase letters to lowercase, displaying the result. Make no changes to any other
    character. Have the program stop when the user presses the period key. At the end, have the
    program display the number of case changes that have taken place.؟
    what is the answer in c#?

Not only are you a year late with that answer but the answer is just wrong. There is no guarentee that the letters are separated by a value of 32. It all depends on the character set used. Use the standard function toupper() and tolower() to convert from upper to lower or vice versa.

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.