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

This is a discussion on Invert characters will convert all non capital letters to '?' within the C Programming forums, part of the General Programming Boards category; Sorry for asking this :/ Purpose : Invert every single letter to uppercase or downcase depending what is it's state. ...

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

    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
    50
    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,675
    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,299
    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,675
    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, 12:21 PM
  2. Capital Letters?
    By MiroMage in forum C Programming
    Replies: 8
    Last Post: 11-05-2008, 03: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, 02:10 AM
  5. check for capital letters
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2002, 09:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21