8 Replies - 1345 Views - Last Post: 30 March 2013 - 08:45 AM Rate Topic: -----

#1 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Convert lowercase to Uppercase

Posted 29 March 2013 - 11:07 AM

This is easy, I know. Here is what I have. Just need to know how to pass the value from main to the function.


#include <iostream>
using namespace std;


char uppercase (char ch) {
if ((ch >= 'a') && (ch <= 'z')) {
return ch - 'a' + 'A';
}
else {
return ch;
}
}

void main() {
	cout<< "Enter a letter for conversion: ";
	cin>> char;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Convert lowercase to Uppercase

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Convert lowercase to Uppercase

Posted 29 March 2013 - 11:12 AM

cin>> char;


This is wrong. The right operand of >> needs to be a variable (or other l-value) -- not a type. You need to define a variable of type char and then use that variable as the right operand of >>. You can then simply pass that variable to your function.

PS: The correct return value for main is int, not void.

This post has been edited by sepp2k: 29 March 2013 - 11:12 AM

Was This Post Helpful? 1
  • +
  • -

#3 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Convert lowercase to Uppercase

Posted 29 March 2013 - 11:21 AM

Nevermind...figured it out. Sorry


#include <iostream>
using namespace std;

char letter;

char uppercase (char ch) {
if ((ch >= 'a') && (ch <= 'z')) {
return ch - 'a' + 'A';
}
else {
return ch;
}
}

void main() {
	cout<< "Enter a letter for conversion: ";
	cin>> letter;
	cout<< "Your letter in uppercase form is: "<<uppercase(letter)<<endl;
}



View Postsepp2k, on 29 March 2013 - 06:12 PM, said:

cin>> char;


This is wrong. The right operand of >> needs to be a variable (or other l-value) -- not a type. You need to define a variable of type char and then use that variable as the right operand of >>. You can then simply pass that variable to your function.

PS: The correct return value for main is int, not void.


Didn't see your reply until after I sent mine. Thanks, I realized that and smacked myself for my Homer Simpson moment. :)
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Convert lowercase to Uppercase

Posted 29 March 2013 - 11:24 AM

You should probably define the letter variable inside main, not globally. There's no reason for it to be global -- it isn't accessed anywhere other than main.
Was This Post Helpful? 0
  • +
  • -

#5 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Convert lowercase to Uppercase

Posted 29 March 2013 - 11:32 AM

Ahh good point. I'll do that. You wouldn't happen to know anything about PEP/8 would you? I have spent an eternity trying to convert a C++ program to it, and I can't get the recursive function to work correctly.
Was This Post Helpful? 0
  • +
  • -

#6 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Convert lowercase to Uppercase

Posted 29 March 2013 - 07:34 PM

Just so you know, the fastest way to convert lower case to uppercase is to simply "and" the lower case character with the number 223. For example, letter = letter&223; If you "and" a character with 223, then if it was already an uppercase letter it will remain unchanged. If it is a lowercase letter, then it will convert it to uppercase. This happens because a single bit is flipped (the bit at bit position 32) and causes a lowercase letter to be decreased in value by 32. For a uppercase letter, the bit is already a 0 and doesn't change. Here is how you could have written your code:

#include<iostream>
using namespace std;

int main()
{  
   char letter;
   cout << "\n\nEnter a letter for conversion: ";
   cin >> letter;
   letter&= 223;
   cout << "\nYour letter in uppercase form is: " << letter << endl << endl;
   
   return 0;
}

This post has been edited by Mrk: 29 March 2013 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: Convert lowercase to Uppercase

Posted 30 March 2013 - 06:20 AM

Actually since you are using a char, which on most systems is a signed type, shouldn't you be using 95 not 223? The value 223 is outside the range of a signed char, and overflowing a signed integral value is undefined. And you really should be checking that the value is indeed an alpha character before you do this conversion. And remember this is only guaranteed to work with the ASCII character set.

Jim

This post has been edited by jimblumberg: 30 March 2013 - 02:06 PM

Was This Post Helpful? 2
  • +
  • -

#8 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Convert lowercase to Uppercase

Posted 30 March 2013 - 08:20 AM

View Postjimblumberg, on 30 March 2013 - 07:20 AM, said:

Actually since you are using a char, which on most systems is a signed type, shouldn't you be using 95 not 223? The value 223 is outside the range of a signed int, and overflowing a signed integral value is undefined. And you really should be checking that the value is indeed an alpha character before you do this conversion. And remember this is only guaranteed to work with the ASCII character set.

Jim

I'll be a son of a gun! You're right, even though 223 works, it messes with the sign bit which I forgot about. So using 95 is better. If range checking / filtering has already been done by the software earlier, than using this method is literally 25 times faster than using toupper().
Was This Post Helpful? 0
  • +
  • -

#9 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Convert lowercase to Uppercase

Posted 30 March 2013 - 08:45 AM

Very cool. I did not know that. I actually wrote this with an assembly program in mind, which I will post when I sit down at my computer. The task was to convert a c++ program to assembly, and I has to write both of them and ensure they trace the same.

This post has been edited by iburres: 30 March 2013 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1