Thread: toUpper

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    33

    toUpper

    Morning,

    with this program, I prompt the user to enter a character and if it is a lower case letter, it would return its uppercase value.

    Here is my code:
    Code:
    int toUpper(int x);
    
    main()
    {
    	char c;
    	
    	printf("Enter character: ");
    	scanf("%c", &c);
    
    	printf("Uppercase = %c\n", toUpper(c));	
    }
    
    int toUpper(int x)
    {
    	char upper;
    
    	if(x >= 'a' && x <= 'z')
    	{
    		upper = x - 32;
    		  return upper;
    	}
    
    }
    It definitely works for lowercase letters. But for some reason, conditions that doesn't fit the boolean expression
    Code:
    if(x >= 'a' && x <= 'z');
    still would execute.

    For instance here are two sampled runs:

    Code:
    Enter character: b
    Uppercase = B
    Code:
    Enter character: B
    Uppercase = "
    B in decimal is 66 and " in decimal is 34 which 66 - 34 = 32, which shows that it executes. Is there an error in my boolean expression that allows this to happen? I even tried changing the conditions to decimal values but still produces the same outcome.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    So maybe pass c as a char not an int to the function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    33
    Quote Originally Posted by claudiu View Post
    So maybe pass c as a char not an int to the function.
    Tried that as well. Same result :/

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Try to return x at the bottom of the function, if the function is receiving an upper case letter it returns nothing.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    33
    Quote Originally Posted by Subsonics View Post
    Try to return x at the bottom of the function, if the function is receiving an upper case letter it returns nothing.
    The problem is that it's returning a value for all sorts of characters. Here's another sample run:

    Code:
    Enter character: _
    Uppercase = ?
    the decimal for '_' underscore is 95
    the decimal for '?' question mark is 63

    which is the difference of 32.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by aromash View Post
    The problem is that it's returning a value for all sorts of characters. Here's another sample run:

    Code:
    Enter character: _
    Uppercase = ?
    the decimal for '_' underscore is 95
    the decimal for '?' question mark is 63

    which is the difference of 32.
    Compile with warnings on (-Wall in GCC) and you get a:

    "warning: control reaches end of non-void function"

    It is undefined behavior afaik if not c99, so, it will return anything it feels like.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to preset upper to the character so that it returns it if it's already upper case.

    Code:
    int toUpper(char x)
    {
    	char upper = x;
    
    	if(x >= 'a' && x <= 'z')
    	{
    		upper = x - 32;
    	}
         return upper;
    }

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Strictly speaking it's not needed, what is needed is to return something instead of relying on chance. This would work equally well for example.

    Code:
    int toUpper(int x)
    {
            char upper;
    
            if(x >= 'a' && x <= 'z')
            {
                    upper = x - 32;
                    return upper;
            }
            return x;
    }
    Or this for that matter.

    Code:
    int toUpper(int x)
    {
            if(x >= 'a' && x <= 'z')
                    return x-32;
            return x;
    }

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    i think it is more easy to use the toupper() function to get the result you want.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I assume there's a reason you aren't just using the built in one. So let's assume you're supposed to write your own.
    Code:
    int mytoupper( int c )
    {
        switch( c )
        {
            case 'a': return 'A';
            case 'b': return 'B';
            ...
            case 'z': return 'Z';
            default: return c;
        }
        return c;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The code you posted in post#1 should work for ascii char set.
    But the reason it does not work is because you are not returning anything if condition is false.
    Your compiler should give you warning. It seems like you are not paying attention to them...

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Subsonics View Post
    Strictly speaking it's not needed, what is needed is to return something instead of relying on chance. This would work equally well for example.

    Code:
    int toUpper(int x)
    {
            char upper;
    
            if(x >= 'a' && x <= 'z')
            {
                    upper = x - 32;
                    return upper;
            }
            return x;
    }
    Or this for that matter.

    Code:
    int toUpper(int x)
    {
            if(x >= 'a' && x <= 'z')
                    return x-32;
            return x;
    }
    Yep... lots of ways to do this one, that's for sure.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    i think it is more easy for you to use the function toupper() in ctype.h to do what you want. try it, friend.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. toupper() function
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2008, 10:54 AM
  2. toupper Function Problems!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2006, 01:40 PM
  3. need example of toupper()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 10:07 AM
  4. Help Converting a String to uppercase using TOUPPER()
    By frgmstr in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2001, 01:56 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM