#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0

    Array of ascii lower case letters a-z C++


    I am new to C++ and want to create an array of just the lower case ascii characters a-z. The ascii code is 97 through 122. Is this possible?
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Yes, this is possible.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0
    Ok so now that I know it is possible ... well I can write an array

    char ascii[121]

    but how do I write an array for char[97-121]?
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    there really isn't 121 elements in the array, only 26 so it should be
    char ascii[27]; (always add one to your array length for termination)
    Then you just loop through and add the values to the array.

    Although this smells a lot like homework, here is an example in C++
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void) {
    	char ascii[27];
    	char temp = '\0';
    	int cntr = 0;
    
    	ascii[0] = 'a';
    	for(cntr = 1; cntr < 26; cntr++) {
    		temp = ascii[cntr - 1];
    		temp++;
    		ascii[cntr] = temp;
    	}
    
    	for(cntr = 0; cntr < 26; cntr++) {
    		cout << "Position " << cntr << " = " << ascii[cntr] << endl;
    	}
    
    	return 0;
    }
    Last edited by Onslaught; September 5th, 2003 at 10:52 PM.
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by Onslaught
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void) {
    	char ascii[27];
    	char temp = '\0';
    	int cntr = 0;
    
    	ascii[0] = 'a';
    	for(cntr = 1; cntr < 26; cntr++) {
    		temp = ascii[cntr - 1];
    		temp++;
    		ascii[cntr] = temp;
    	}
    
    	for(cntr = 0; cntr < 26; cntr++) {
    		cout << "Position " << cntr << " = " << ascii[cntr] << endl;
    	}
    
    	return 0;
    }
    That seems unnecessarily complicated to fill an array with the 26 lower case letters. It's perfectly acceptable to have simply an array of characters with no terminating null, which is only really required if you're treating the array as a c-style string, typically to be used with functions from <cstring> such as strlen(), strcmp() etc. You can populate (and display) an array with lower case characters thus:
    Code:
    #include <iostream>
    int main()
    {
        char letters[26];
        for(int i=0; i<26; ++i) letters[i] = 'a' + i;
        for(int i=0; i<26; ++i) std::cout << letters[i];
        std::cout << std::endl;
    }
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Or simply:
    Code:
    char array[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
                     'n', 'o', 'p', 'q', 'r', 's', 't','u', 'v', 'w', 'x','y', 'z' } ;
    This is cumbersome, but has no run-time overhead.

    You could also do:
    Code:
    char array[] = "abcdefghijklmnopqrstuvwxyz" ;
    But this will have 27 elements, the last being a nul string terminator. But that may not matter in your application, and the syntax is less cumbersome.

    If you need a programatic approach:
    Code:
    char ch ;
    char array['z' - 'a' + 1] ; // or 26, but just to be really robust!
    
    for( ch = 'a'; ch <= 'z'; ch++ )
    {
        array[ch - 'a'] = ch ;
    }
    BigBadBob's loop is less complicated, but makes the assumption that there are 26 characters in the alphabet! :D

    Clifford
    Last edited by clifford; September 6th, 2003 at 08:49 AM.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Just adding another angle to this discussion. If you plan to use that array, solely for the purpose of looking up whether a character is a lowercase letter or not, there's no need to declare an array at all. Simply use the islower() function in this case.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    There was a reason why I made it just a little overly complicated. It smells just a little too much like homework.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    Using a function just to check whether a letter is lower case or not seems pretty lazy to me, it only takes 2 conditions & you'll be increasing efficiency.

    I would go for clifford's first or second approach, it will potentially save a "lot" of processing time.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Originally posted by xtor
    Using a function just to check whether a letter is lower case or not seems pretty lazy to me, it only takes 2 conditions & you'll be increasing efficiency.
    islower() is locale safe. Also a test like this is a bad idea for EBCDIC char set:
    Code:
    if (c >= 'a' && c <= 'z') {
        /* This is a bad idea and won't work for EBCDIC or non-english char sets */
    }
    If you look at this page, the reason is fairly obvious. Hope this helps :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  20. #11
  21. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    quick point,

    1. there will be 26 letters to put in the array, so the array does not need to be dynamically allocated.

    2. it would be more practical (produce a better learning experience) to test every character and add only the ones
    needed.

    These are the points of this exercise.

    Code:
    for each possible character
          test if letter (and not number or punctuation)
                test if lowercase
                       add to array
    isalpha() and islower() are all you need
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  22. #12
  23. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally posted by Scorpions4ever
    [B]islower() is locale safe. Also a test like this is a bad idea for EBCDIC char set:
    Code:
    if (c >= 'a' && c <= 'z') {
        /* This is a bad idea and won't work for EBCDIC or non-english char sets */
    }
    True but the OP explicitly said ASCII in his requirements specification.

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo