shorter way to represent alphabet

This is a discussion on shorter way to represent alphabet within the C++ Programming forums, part of the General Programming Boards category; Hey everyone i have a piece of code here that i typed out all the letters of the alphabet. Code: ...

  1. #1
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20

    shorter way to represent alphabet

    Hey everyone i have a piece of code here that i typed out all the letters of the alphabet.

    Code:
    string::size_type pos = name.find_first_not_of("qwertyuiopasdfghjklzxcvbnm'QWERTYUIOPASDFGHJKLZXCVBNM " );
    however with the exception of the ' and the trailing space in the set the rest is the alphabet. is there a shorter way to represent this? the reason i ask is because i dont like the long statement in my code. I know there are things like isalpha() etc but i dont know if there is a similar representation for situations like this.

    if not then i guess i would have to live with it like this:
    Code:
    string::size_type pos = name.find_first_not_of
    			("qwertyuiopasdfghjklzxcvbnm' QWERTYUIOPASDFGHJKLZXCVBNM" );

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Like
    Code:
    const char *alphabet = "qwertyuiopasdfghjklzxcvbnm' "
                           "QWERTYUIOPASDFGHJKLZXCVBNM";
    
    string::size_type pos = name.find_first_not_of( alphabet );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there a shorter way to represent this?
    Shorter how? Horizontally? A much better solution would be to read a configuration file with the alphabet so that it isn't hardcoded:
    Code:
    string get_alphabet ( string file_name )
    {
      ifstream in ( file_name.c_str() );
      string alphabet;
    
      if ( in ) {
        // Fill the string according to the file format
      }
      else
        throw runtime_error ( "File open failure" );
    
      return alphabet;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Most likely, all find_first_not_of does is loop through each character until it finds one not in that list. Since the list is long it might make more sense to use your own loop and check for isalpha, '\'' and ' '. If you want to do it in one line make a function that checks for those characters and use it with the find_if algorithm.

  5. #5
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    hmm. all good ideas, thanks. i just implemented what salem suggested though, it was the easiest to do as i did not want to have to change too much code. there is another part where i would need to input numbers alone, i think i may try to use a loop with find_if and isdigit.
    or i might just stringstream the number to an int and then use find_first_not_of again.

    wait thats the first time i saw pointers with letters. by that i mean i always see pointers pointing to a value like 34 or an address like 3421 or some integer. up to today i have not found any pointer tutorial using anything but int variables. could somebody explain the const char *alphabet to me so that hopefully i would be able to understand it and use it in future.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    A string literal (characters between double quotes) are represented in C++ as an array of char. One way to reference an array is through a pointer, and char* is the C method of handling strings. Since a string literal is a constant string that cannot be changed, it has the type if const char* in C++. So if you ever want a constant string "variable" you can use const char* and assign the double-quoted string.

    This is also just fine (and the const is good practice but not necessary here):
    Code:
    const string alphabet = "qwertyuiopasdfghjklzxcvbnm' "
                           "QWERTYUIOPASDFGHJKLZXCVBNM";
    
    string::size_type pos = name.find_first_not_of( alphabet );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number or Alphabet
    By Ahmed29 in forum C++ Programming
    Replies: 10
    Last Post: 10-28-2008, 04:50 AM
  2. [help]count alphabet
    By PUI in forum C Programming
    Replies: 12
    Last Post: 09-25-2006, 07:17 AM
  3. Which characters represent EOF in windows?
    By hitesh1511 in forum C Programming
    Replies: 10
    Last Post: 08-11-2006, 12:07 PM
  4. How do you represent non-integer base numbers?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 04-08-2004, 01:09 PM
  5. i,j, n,m, x,y,z ... We dont know the alphabet
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-20-2002, 04:46 AM

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