我试图使用数组从头开始创建一个strtok函数

时间:2016-04-19 08:44:26

标签: c++

我不知道从哪里开始。我试图使用数组从头开始执行strtok函数。我希望将每个令牌存储在临时数组中,打印它,并继续为数组所拥有的令牌执行此操作。例如,一个字符串,例如“this is fab”,我希望“this”被复制到temp,直到遇到分隔符(null,或空格或任何字符)。然后打印。一切的最终结果应该是:

这个\ n

是\ n

fab \ n

有没有人有任何见解。

char strtok( char s[], char key)
{
char find;
char temp[100] = " ";

int i = 0;
while( s[i] != '\0')
{
    i++;
    for(; s[find] != key && s[find] != '\0'; find++)
    {
        temp[i]= s[find];
    }
            cout<<endl;

    }

for( int x = 0; temp[x] != '\0'; x++)
    {
        cout<<temp[x];
    }

    cout<<endl;
}

2 个答案:

答案 0 :(得分:1)

标准strtok定义如下:

char *strtok(char *str, const char *delim)

对于自制版,必须返回char*。下面的示例与标准版本类似,但它使用char作为分隔符。

另见strtok

的示例

在第一次调用中,函数应该设置变量。在随后的调用中,它应该在之前停止的地方继续。

char *homemade_strtok(char *arg, const char delimiter)
{
    static int pos = 0;
    static char* buf = 0;
    static char* token = 0;

    if (arg)
    {
        //start a new search
        pos = 0;
        buf = arg;

        //delete previous token if any
        if (token) delete token;

        //create token at least as big as the argument
        token = new char[strlen(buf) + 1];
    }

    if (!buf)
        return 0;

    if (pos >= strlen(buf))
        return 0;

    //prepare the token
    memset(token, 0, strlen(buf) + 1);

    int i = 0;
    while (pos < strlen(buf))
    {
        if (buf[pos] == delimiter)
        {
            pos++;
            return token;
        }
        token[i] = buf[pos];
        i++;
        pos++;
    }

    return token;
}

int main() 
{
    char *buf = "hello world 1a 2b 3c";
    char *token = homemade_strtok(buf, ' ');
    while (token)
    {
        std::cout << token << "\n";
        token = homemade_strtok(NULL, ' ');
    }
    return 0;
}

编辑:修复内存泄漏

答案 1 :(得分:1)

如果您需要自己的strtok版本并保存结果值,可以使用下一个代码:

//+------------------------------------------------------------------+
#include <memory.h>
#include <stdio.h>

char *mystrtok(char *str,char *delim)
  {
   char *result;
   char *ptr;
   char *delimIter;
   int val;
   char *delimVal;
   static char *last;

   result=str;
   if(str || (result=last)!=0)
     {
      lbl:
      ptr=result + 1;
      delimIter=delim;
      while(*(++delimIter - 1))
        {
         if(*result==*(delimIter - 1))
           {
            ++result;
            goto lbl;
           }
        }
      if(*result)
        {
         lbl2:
         val=(int)(ptr + 1);
         delimVal=delim;
         while(*ptr!=*(++delimVal - 1))
           {
            if(!*(delimVal - 1))
              {
               ++ptr;
               goto lbl2;
              }
           }
         if(*ptr)
            *ptr=0;
         else
            val=0;
         last=(char*)val;
        }
      else
        {
         last=0;
         result=0;
        }
     }
   else
     {
      result=0;
     }
   return result;
  }

static char results[1024]={0};
static int pos = 0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int main()
  {
   char str[]="abcd eeeee wwwwww1 - 123456, 222222 6gfrghrtwe.";
   char * pc=mystrtok(str," ,.-");
   while(pc!=NULL)
     {
      printf("%s\n",pc);
      pc=mystrtok(NULL," ,.-");
      if(pc!=NULL)
        {
         int size=strlen(pc);
         memcpy(&results[pos],pc,size);
         // insert delimiter
         pos+=size;
         results[pos]=',';
         pos++;
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+