Thread: parsing words out to pointer array

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy parsing words out to pointer array

    Hello,

    I hope someone can help me, I'm a little bit stuck.
    It's been a while since I did any coding and this project is designed to get me back into it.

    Basically I want to take a string in from the user then break this string up into separate words, then store these words as an array of pointers to strings(or char arrays);

    Here's what I have, it compiles and runs but gives an unhandled exception...

    //code begin

    #include <iostream>
    #include <string.h>

    using namespace std;

    bool isalfa(char ch);

    void Split_String(char * Origin, char * Destination_array);

    int main()

    {

    char Array_Source[512];
    char * Array_of_Char_Poiners[50];

    cout<< "please enter a question string: \t";
    cin.getline(Array_Source, 512);

    Split_String(Array_Source, Array_of_Char_Poiners[50]);

    for(int a=0; a<=50; a++)

    cout << Array_of_Char_Poiners[a] << "\n";

    return 0;
    }





    void Split_String(char * Origin, char * Destination_array){

    char * p1;

    int i, j, k = 0;



    for(i=0; i<=512; i++){
    if(!isalfa(Origin[i]))
    i++;

    p1=new char[50];
    j=0;

    if(isalfa(Origin[i])){
    p1[j] = Origin[i];
    j++;
    }

    }
    }


    bool isalfa(char ch){

    if(ch>33 && ch<122)

    return true;

    else
    return false;
    }

    //code ends

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895

    Re: parsing words out to pointer array

    I tried to mend the code, but seriously, it is beyond repair.

    I suggest you do it again using std::vector and std::string. Here's a splitter function.
    Code:
    #include <vector>
    #include <string>
    using std::vector;
    using std::string;
    
    // this takes a string and splits it into an array of smaller strings
    // it's splitted every time split_at is encountered
    void split_string(const string &s, vector<string> & out, char split_at)
    {
    	// for searching the string
    	string::const_iterator stay, run;
    	// set stay to the start of the string
    	stay = s.begin();
    	// temporary
    	string t;
    	// loop run through the string, from front to back
    	for(run=stay; run != s.end(); ++run)
    	{
    		// if encountered split char...
    		if(*run == split_at)
    		{
    			// ...clear the temporary value...
    			t.erase();
    			// (speeds it up a little)
    			t.reserve(run-stay);
    			// ..., copy current substring to temporary...
    			t.append(stay, run);
    			// ...and add it to the array.
    			out.push_back(t);
    			// Finally, let the next substring begin directly after the splitting char.
    			stay = run+1;
    		}
    	}
    	// same as above for the last piece (from last split char to end of string)
    	t.erase();
    	t.reserve(run-stay);
    	t.append(stay, run);
    	out.push_back(t);
    }
    Alternativly, grab boost::string_tokenizer from www.boost.org
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And a single tip: array indices are 0-based, so they go up to (size-1). The array iteration loop uses <, not <=.
    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

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Thanks CornedBee,

    Is it really that bad?

    Thanks for the reply, I'll read through your solution now.

    M

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The Split_String function is a total chaos.
    And it allocates an awful lot of memory where at worst more than 99% is wasted.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  3. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM