Thread: A few quick questions

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    A few quick questions

    Hi all, I'm new to C, and I'm just beginning to experiment.

    What I want to do it take an input, write it to an array, then print only the lowercase letters or the capital letters.

    Also, with the same array, is it possible to print only the Nth letter?

    Thanks in advance,
    Brodie.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, writing the nth letter of a string (character array) is done by displaying str[n-1] (0 being the first letter, so nth letter is n-1).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    you can check each character if it belongs to uppercase range or lowercase range and branch accordingly

    that means,
    if you want to see if letter is uppercase or not
    Code:
    if(ch>=65 && ch<=90)
    {
       //its upper case
    }
    another method is to use isupper() or islower() functions in ctype.h library

    <ctype.h>

    to print only nth letter you have to print element having its index as (n-1)
    Last edited by creeping death; 03-30-2009 at 03:24 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creeping death View Post
    another method is to use isupper() or islower() functions
    it is preffered method as it will work for any encoding even that has no sequential coding for characters

    also - do not use magic numbers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by vart View Post
    it is preffered method as it will work for any encoding even that has no sequential coding for characters

    also - do not use magic numbers

    yes, you should define the range as something like

    Code:
    #define UPPER_START 65
    #define UPPER_END 90
    and then use UPPER_START and UPPER_END in place of actual numbers...

    my bad.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    just 'A' and 'Z' would do...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd suggest using the characters if you are going to do your own comparison:
    Code:
    if(ch>='A' && ch<='Z')
    {
       //its upper case
    }
    That way, we don't have to look it up in the ASCII table to figure out what it is. And it has a remote chance of working even if the character encoding is NOT ASCII (although it presumes that all letters in the alphabet are in order [which it is in ASCII, and ALMOST in EBCDIC but there are gaps in EBCDIC].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or just use isalpha() from ctype.h that way it doesn't matter at all

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Or just use isalpha() from ctype.h that way it doesn't matter at all
    islower or isupper if you want to check if it's lower/upper case - yes, that'll work for BOTH ASCII and EBCDIC along with any other character encoding where the compiler/library supports standard C functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by creeping death View Post
    yes, you should define the range as something like

    Code:
    #define UPPER_START 65
    #define UPPER_END 90
    and then use UPPER_START and UPPER_END in place of actual numbers...

    my bad.
    I think this is following the letter of the rule and not the spirit, since 65 and 90 are still magic numbers; and 'A' and 'Z' are still more readable (in context, UPPER_START makes sense, but looking at this define line without seeing it used, I'm not sure I would recognize the numbers).

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Hey.. thanks alot.. but as I'm very new, I'm having trouble adding a string of variable length to said array...


    The idea is to be able so print, say the capitals, the lowercase and the integers in a given string, all of different lines

    Thanks,
    Brodie.
    Last edited by Brodie337; 04-06-2009 at 12:58 AM.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Brodie337 View Post
    Hey.. thanks alot.. but as I'm very new, I'm having trouble adding a string of variable length to said array...


    The idea is to be able so print, say the capitals, the lowercase and the integers in a given string...

    Thanks,
    Brodie.
    string basically is an array of chars - so you can assign it char by char... just add the nul-character at the end.

    if you need the more complex formating - read about sprintf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Allright... I can get a string of characters into an array... Now I need to get the capitals out of said array...
    What I'm using looks like this:
    Code:
        while (isupper (msg))
            putchar(out[j++]);
    But I get the error message when colpiling:
    Code:
    In function `int main()':|
    error: invalid conversion from `char*' to `int'|
    error:   initializing argument 1 of `int isupper(int)'|
    Can someone shed some light on this?
    Last edited by Brodie337; 04-06-2009 at 07:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two quick questions about open file dialog boxes
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 04-05-2005, 08:49 AM
  2. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  3. A quick question(s)
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2003, 09:39 PM
  4. A few quick questions...
    By cpp4ever in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2001, 09:28 AM