Thread: Invert characters will convert all non capital letters to '?'

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Invert characters will convert all non capital letters to '?'

    Sorry for asking this :/

    Purpose : Invert every single letter to uppercase or downcase depending what is it's state.

    Problem : Invert characters will convert all non capital letters to '?'

    Notice : i am pretty sure that the function receives the characters as it supposed to.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    char invert(char c)
    {
         if(isalpha(c)==0)/*not a letter*/
              return c;
         if('c'<=132)/*then it is uppercase.remember we know it is a letter*/
             c+=32;
         else
            c-=32;
    
         printf("edw %c\n",c);
    
         return c;
    }
    After-question : is alpha safe to use? Because this isalpha - C++ Reference made me a little bit worried(the section that says about locales)

    Thank you in advance and sorry for the question

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    If you want this: "hello, WORLD" -> "HELLO, world", look at line 8.

    You are using 'c' instead of the variable c. Double check your ascii values, I'd probably use ascii value dec (90) instead of oct (132)?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Excellent.Problem solved.However the question about the isaplha still remains.

    Thank you fnprintf.Now i can go to bed with a smile

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could also use if(c < 'a') since you know that you have passed isalpha() at that point. There is also toupper() and tolower() functions in ctype.h

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You should not be using any magic numbers like 32 or 132 in that program.
    E.g. use - 'A' + 'a' to go from uppercase to lowercase and - 'a' + 'A' to go from lowercase to uppercase.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    subsonics i did not want to use them,in order to play a little with the characters.

    iMalc you are right about the ascii codes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why capital letters in function
    By vrkiller in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2011, 01:21 PM
  2. Capital Letters?
    By MiroMage in forum C Programming
    Replies: 8
    Last Post: 11-05-2008, 04:32 PM
  3. strcmp, a problem with capital letters
    By RichardH in forum C Programming
    Replies: 11
    Last Post: 04-23-2007, 08:49 AM
  4. Capital Letters ...
    By twomers in forum C++ Programming
    Replies: 11
    Last Post: 01-11-2006, 03:10 AM
  5. check for capital letters
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2002, 10:32 PM