需要有关如何正确使用strtok()的建议

时间:2015-07-09 17:17:56

标签: c

我一直在研究我的c编程课程中的一个问题。经过几天的辛苦工作,我决定在这里寻求退伍军人的建议。

strtok()使用此函数我无法获得我在下面发布的所需输出。请提示我可以做出哪些更改。

#include<string.h>
#include<stdlib.h>
#include<stdio.h>

int main( )

{

int i,j,k;

char str[50] =":FIRSTNAME:Krishna::MIDDLENAME:Prasad::LASTNAME: Iyer::EMAIL:kpIyer@microsoft.com::CARNO:1221 YC 90PA:.";

char *p,*q,*r,*z;     //character array to store each substring name;


char seps[] =":";    //delimiter : or::

p=(char*)malloc(70); //allocating memory for substring arrays
q=(char*)malloc(70);
r=(char*)malloc(70);
z=(char*)malloc(70);

p= strtok(str,seps );//strtok for first string
q=strtok(NULL,seps );
r= strtok(NULL, seps );
z= strtok(NULL, seps );

puts(p);
puts(q);
puts(r);
puts(z);

sprintf(z,"%s %s %s",p ,q ,r);//appending all the strings into one single array.
//puts(z);


return 0;

}
}

我需要的输出是“Krishna Prasad Iyer”但不幸的是我得到了

FIRSTNAME
krishna
MIDDLENAME
prasad

请建议我可以改变什么......

1 个答案:

答案 0 :(得分:1)

首先,我评论你的程序,以显示它的一些错误。

#include<string.h>
#include<stdlib.h>
#include<stdio.h>

int main( )
{
    int i,j,k;
    // here the array is not big enough
    char str[50] =":FIRSTNAME:Krishna::MIDDLENAME:Prasad::LASTNAME: Iyer::EMAIL:kpIyer@microsoft.com::CARNO:1221 YC 90PA:.";
    char *p,*q,*r,*z;
    // here the delimiters are wrong, because of the space before "Iyer"
    char seps[] =":";

    // here you needlessly allocate memory
    p=(char*)malloc(70);
    q=(char*)malloc(70);
    r=(char*)malloc(70);
    z=(char*)malloc(70);

    // here you overwrite the pointers returned by malloc()
    // and you dont check their value for NULL
    p= strtok(str, seps );
    q= strtok(NULL, seps );
    r= strtok(NULL, seps );
    z= strtok(NULL, seps );

    puts(p);
    puts(q);
    puts(r);
    puts(z);

    // here you are overwriting a fragment of your input string
    // assuming z != NULL from malloc()
    sprintf(z,"%s %s %s",p ,q ,r);
    return 0;
}

现在这里是一个编辑,评论它的不同之处。请注意,当您提取数据中包含空格的字段(例如字段CARNO)时,此方法无法正常工作。这是strtok()方法的缺点:您不能将空格作为分隔符作为数据的一部分。您必须从' '中删除seps[]并清除子字符串中的前导和尾随空格。

#include<string.h>
#include<stdlib.h>
#include<stdio.h>

int main( )
{
    // let the compiler determine the array size
    char str[] =":FIRSTNAME:Krishna::MIDDLENAME:Prasad::LASTNAME: Iyer::EMAIL:kpIyer@microsoft.com::CARNO:1221 YC 90PA:.";
    char *p,*q,*r,*z;
    char seps[] =" :";              // added 'space' to delimiters array

    z= strtok(str, seps );          // read first field identifier
    if (z == NULL)
        exit (1);
    if (strcmp(z, "FIRSTNAME"))     // check the field name
        exit (1);
    p= strtok(NULL, seps );         // read first data field
    if (p == NULL)
        exit (1);

    z= strtok(NULL, seps );         // read second field identifier
    if (z == NULL)
        exit (1);
    if (strcmp(z, "MIDDLENAME"))    // check the field name
        exit (1);
    q= strtok(NULL, seps );         // read second data field
    if (q == NULL)
        exit (1);

    z= strtok(NULL, seps );         // read third field identifier
    if (z == NULL)
        exit (1);
    if (strcmp(z, "LASTNAME"))      // check the field name
        exit (1);
    r= strtok(NULL, seps );         // read third data field
    if (r == NULL)
        exit (1);

    printf("%s %s %s\n", p ,q ,r);
    return 0;
}

节目输出:

Krishna Prasad Iyer