Nov 8, 2009

Alternative To Strtok() Function In C++

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Alternative To Strtok() Function In C++

vptr
Hello,
i have written an alternative function to strtok(), which i think is better and easier to use. I hope it
will be usefull. Here is the code snippet from my program:

CODE
     1 /*              
     2  * The Ultimate Network Tool.
     3  * Copyright (C) 2004 Virtual Pointer
     4  *
     5  * The Ultimate Network Tool is free software; you can redistribute it and/or
     6  * modify it under the terms of the GNU General Public License
     7  * as published by the Free Software Foundation; either
     8  * version 2.1 of the License, or (at your option) any later version.
     9  *              
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * General Public License for more details.
    14  *              
    15  * You should have received a copy of the GNU General Public
    16  * License along with this program; if not, write to the Free
    17  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    18  * 02111-1307 USA.      
    19  */                            
    20                                
    21 #include <stdio.h>              
    22 #include <stdlib.h>                    
    23 #include <string.h>                    
    24                                                
    25 /* sample:                                      
    26  *                                              
    27  * char **tokens;                              
    28  * ex_tokens("some string", ' ', &tokens);
    29  *                                      
    30  * while (*tokens)                      
    31  * {                                            
    32  *      printf("%s\n", *tokens);                
    33  *      tokens++;                              
    34  * }                                    
    35  *                              
    36  * free(*tokens);              
    37  */                            
    38                                        
    39 int ex_tokens(char *string, char c, char ***tokens)
    40 {                                      
    41         int i, toks = 0;        
    42         char **tmp_tokens;
    43         char *tmp_string;
    44                        
    45                        
    46         if (string == NULL)    
    47                 return (0);    
    48                                        
    49         for (i = 0; i < strlen(string); i++)
    50         {                      
    51                 if (string[i] == c)
    52                         toks++;
    53         }
    54
    55         if (toks)
    56         {
    57                 toks++;
    58
    59                 if ( (tmp_string = (char *)malloc(strlen(string)+1)) == NULL)
    60                         return (0);
    61
    62                 memset(tmp_string, 0, strlen(string)+1);
    63
    64                 if ( (tmp_tokens = (char **)calloc(toks, sizeof(char *))))
    65                 {
    66                         toks = 0;
    67                         for (i = 0; i < strlen(string); i++)
    68                         {
    69                                 /* cia toks patikrinimas tam jei stringas prasideda skirtuku */
    70                                 if (string[i] == c && strlen(tmp_string))
    71                                 {
    72                                         if ( (tmp_tokens[toks] = (char *)malloc(strlen(tmp_string)+1)))
    73                                         {
    74                                                 memset(tmp_tokens[toks], 0, strlen(tmp_string));
    75                                                 strncpy(tmp_tokens[toks], tmp_string, strlen(tmp_string));
    76                                                 memset(tmp_string, 0, strlen(tmp_string)+1);
    77                                                 toks++;
    78                                         }
    79                                         else
    80                                         {
    81                                                 free(tmp_string);
    82                                                 free(tmp_tokens);
    83                                                 return (0);
    84                                         }
    85                                 }
    86                                 else
    87                                 {
    88                                         if (string[i] == c)
    89                                                 continue;
    90                                         strncat(tmp_string, (char*)&string[i], 1);
    91                                 }
    92                         }
    93
    94                         if (strlen(tmp_string))
    95                         {
    96                                 if ( (tmp_tokens[toks] = (char *)malloc(strlen(tmp_string)+1)))
    97                                 {
    98                                         memset(tmp_tokens[toks], 0, strlen(tmp_string));
    99                                         strncpy(tmp_tokens[toks], tmp_string, strlen(tmp_string));
   100                                 }
   101                                 else
   102                                 {
   103                                         free(tmp_string);
   104                                         free(tmp_tokens);
   105                                         return (0);
   106                                 }
   107                         }
   108
   109                         free(tmp_string);
   110                         *tokens = tmp_tokens;
   111                         return (1);
   112                 }
   113                 else
   114                         return (0);
   115         }
   116
   117         return(0);
   118 }

 

 

 


Comment/Reply (w/o sign-up)

r3d
good work wink.gif
thumbs up for the copy and paste protection cool.gif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for alternative, strtok, function, c
advertisement



Alternative To Strtok() Function In C++

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com