smart selection of arrays

This is a discussion on smart selection of arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a string of alphanumeric characters.Iwant to access alphabets in character array and numerals in an int array, ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Qatar
    Posts
    39

    Cool smart selection of arrays

    Hi,
    I have a string of alphanumeric characters.Iwant to access alphabets in character array and numerals in an int array, simultaneously.how should i go ahead.
    thanxxxxxxxx

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'm afraid I don't understand your problem description. Can you elaborate?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    if( isdigit(alphanumstring[index]) )
    {
         // choose numeric array
    }
    else if ( isalpha(alphanumstring[index]) )
    {
         //choose alpha array
    }
    else
    {
         //not alpha or numeric ... do something else
    }

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I think that he wants to break the string into two arrays, one with chars and the other with int.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    #include <algorithm>
    #include <functional>
    #include <string>
    #include <vector>
    #include <ctype>
    
    //inside a function:
    string mixedString;
    
    //fill mixed string with data
    
    vector<int> integerArray;
    vector<char> characterArray;
    
    //put all digits at the end
    string::iterator pos=remove_if(mixedString.begin(), mixedString.end(), ptr_fun(isdigit) );
    
    //copy the digits to int array
    copy(pos, mixedString.end(),back_insert_iterator(integerArray));
    
    //convert ascii digits to numbers
    for_each(integerArray.begin(), integerArray.end(), bind2nd(minus<char>(),'0'));
    
    //copy the non numbers to the character array.
    copy(mixedString.begin(),pos,back_insert_iterator(characterArray));
    Last edited by King Mir; 07-06-2007 at 01:48 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. selection problem
    By Ken JS in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:47 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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