1

我有一行格式如下:

str="WORD1\tWORD2\tWORD3\tWORD4...";

问题是我需要得到第三个词。

我一直在玩 strchr 和 strcpy,但我只是弄得一团糟。

我唯一想做的就是保存在同一个字符串变量 str 中,这样我就可以继续解析直到第三列。

char *p;
p=strchr(str,'\t');
strcpy(str,str,p?);

我会很感激你的帮助。

谢谢!

4

6 回答 6

1

试试这个,只是一个想法(我没有检查其他字符串):

代码没有给出第一个和最后一个字,它只输出子字符串,如果它\t按照你的要求用括号括起来。:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    char* str="WORD1\tWORD2\tWORD3\tWORD4";
    char *p1 = NULL;
    char *p2 = NULL;
    int i = 0, which = 3;
    char word[10]={0};

    scanf("%d", &which );
    p1 = str;
    for(i=0 ; (p1 != NULL) && (i < which-1); i++){
        p1=strchr(p1 + 1, '\t');
        if(p1 +1)
        p2=strchr(p1 + 1, '\t');
    }

    if(p1!=NULL && p2!=NULL){
        strncpy(word, p1, p2-p1);
        printf("\n %s\n", word);
    }
    return 1;
}

它运行如下:

$ ./a.out 
1
:~$ gcc x.c
:~$ ./a.out 
2
    WORD2
:~$ ./a.out 
3
    WORD3
:~$ ./a.out 
4

这是你需要的吗?

于 2013-04-12T06:38:56.463 回答
0

strpbrk()
strspn()
strtok()
strsep()  // define _BSD_SOURCE to have this available

可用于解析。


regcomp()您也可以通过调用and来使用正则表达式regexec()

于 2013-04-12T06:20:54.307 回答
0

(1)

strchr将定位字符串中第一次出现的字符(手册页)。

char *strchr(const char *s, int c);

(2)

strcpy将 source 指向的字符串复制到 destination (手册页)指向的数组中。

char *strcpy(char *dest, const char *src);

(3)

您需要使用strtok来解析您的输入字符串(手册页)。

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

看一个例子:http ://www.cplusplus.com/reference/cstring/strtok/

(4)

您不能将字符串复制到未分配的指针中。您需要为目的地分配内存。

(5)

这是一个示例代码,可以为您解释更多:

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

int main() {
    char *str="WORD1\tWORD2\tWORD3\tWORD4...";
    char *p = NULL;
    char *out = NULL;
    int count = 0;
    int needed = 2; 

    p = strtok(str , "\t");
    while(p && count != needed) {
        p = strtok(NULL , "\t");
        count ++;
    }

    if(p) {
        out = malloc(strlen(p) + 1);
        strcpy(out, p);
        printf("String found: %s\n", out);
    }
    return 0;
}
于 2013-04-12T06:35:45.127 回答
0
char *cut(int nth, char* out, const char* in){
    const char *pi=in;
    char *po=out;
    int n = 0;
    for(;;){
        *po=*pi;
        if(*po=='\t')
            *po='\0';
        if(*po == '\0'){
            ++n;
            if(n==nth)
                return out;
            else if(*pi == '\0')break;
            po=out;
        } else 
            ++po;
        ++pi;
    }
    return NULL;
}
/*
int main() {
    const char *str="WORD1\tWORD2\tWORD3\tWORD4\tWORD5";
    char word[32]={0};
    printf("%s\n", cut(3, word, str));//WORD3
    return 0;
}
*/
于 2013-04-12T09:05:08.247 回答
0

抱歉耽搁了,这就是我最终实现它的方式:

      int main() 
      {

          char *readbuff="WORD1\tWORD2\tWORD3\tWORD4...";
          char * pch;
          int third_col=0;
          pch = strtok (readbuff," \t");
          while ((pch != NULL)&& (TRUE == notfound))
          {
            pch = strtok (NULL, " \t");
            if (third_col==1)
            {
              dprintf (DBG_INFO,"The third word is: %s\n",pch);
              notfound=FALSE;
            }
            third_col++;
          }
    }
于 2013-04-24T07:52:26.540 回答
0

简单的 C++ 代码:

    string s = "abc\tdef\tghi";
    stringstream ss(s);
    string a,b,c;
    ss >> a ; ss.ignore() ; ss >> b ; ss.ignore() ; ss >> c;    
    cout << a << " " << b << " " << c << endl;

输出 :

abc def ghi
于 2013-05-27T14:08:37.907 回答