return character arrays

This is a discussion on return character arrays within the C++ Programming forums, part of the General Programming Boards category; I've been learning C++ for the past few days, and I'm trying to write my own string and StringTokenizer class ...

  1. #1
    Azmeos
    Guest

    return character arrays

    I've been learning C++ for the past few days, and I'm trying to write my own string and StringTokenizer class in order to better understand chars and char arrays (that is why I am not using the predefined string class).

    I can't seem to return this character array from this part of my StringTokenizer class ... can someone help me? Maybe I just don't know the syntax...

    Code:
    char StringTokenizerC::nextToken()
    {
    	m_position++;
    	int j = 0;
    	int p = 0;
    	int start = 0;
    	char* token = new char[MAXLINE];
    	RVString rvs(m_string);
    	int begin = 0;
    	for(int i = 0; i < m_position; i++) {
    	start = rvs.find_first_of(m_string, "\0", begin);
    	begin = start + 1;
    	}
    	int end = rvs.find_first_of(m_string, "\0", begin + 1);
    	for(j = start, p = 0; p < (end - start); j++, p++) {
    		token[p] = m_string[j];									//Adds the characters to the string from the start and end points
    	}
    	return token;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The return type is char, not char* in your code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    IT doesn't allow me to put declare a char* StringTokenizerC::nextToken() though ... so how do you suggest I fix it?

  4. #4
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Nevermind ... heh, I fixed it... sorry for the newbie question.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    string tokenizer function

    I wrote my own strin tokenizer function a while back and here is what I came up with. The only draw back is that you have to specify the length of the strings. In most cases it would work fine since most tokens would be under 255. However in other cases it where the limit of the token may be million characters in length since this would be an unsatisfactory use of memory. At any rate this simple function has the advantage of detecting back to back delimiters as a token where strtok does not recognized them as a token.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    // Return Value: number of tokens read
    // pBuffer - The string where the data is store as a whole
    // chDelimiter - char delimiter that separates the values
    // pTokens string array
    // iMaxTokens - Maximum number of tokens expected
    int StringTokenizer(char* pBuffer, char chDelimiter, char pTokens[][255], int iMaxTokens); 
    
    
    void main( void )
    {
    	char strTokens[10][255];
    	char chDelimiter = '|';
    	char stringline[] = {"JOE|SCHMUCK|1111|ONEWAY||"};
    	int iTokens;
    
    	iTokens = StringTokenizer(stringline, chDelimiter, strTokens, 10);
    
    	
    	for( int i = 0; i < iTokens; i++)
    	{
    		cout << "\nToken " << i + 1 << "." << strTokens[i]; 
    	}
    
    	cout << endl;
    
    
    }
    
    int StringTokenizer(char* pBuffer, char chDelimiter, char pTokens[][255], int iMaxTokens)
    {
    	int i = 0;
    	int j = 0;
    
    	while( *pBuffer != '\0' && i < iMaxTokens)
    	{
    		j = 0;
    		while( *pBuffer != chDelimiter && *pBuffer != '\0')
    		{
    			pTokens[i][j] = *pBuffer;
    			j++;
    			pBuffer++;
    		}
    		pTokens[i][j] = '\0';
    
    		if( *pBuffer == '\0')
    		{
    			i++;
    			break;
    		}
    
    		pBuffer++;
    
    		i++;
    	}
    
    	return i;
    }
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Replies: 1
    Last Post: 04-20-2003, 06:02 PM

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