C语言中通过分隔符来截取字符串

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void reqorder(char *input,char *output,int counter)
 5 {
 6     int i=0, j=0;
 7     char *p=input;
 8     char *buf=NULL;
 9 
10     while((char)(*p)!='\0')
11     {
12         if((char)(*p)=='?')
13         {
14             if(i==counter)
15             {
16                 buf=p+1;
17                 while((char)(*buf)!='\0' && (char)(*buf)!='?')
18                 {
19                     j++;
20                     buf++;
21                 }
22                 strncpy(output,p+1,j);
23                 return;
24             }
25             i++;
26         }
27         p++;
28     }
29 }
30 
31 int main()
32 {
33     char ch[32]="a?b?c?12?34?56?";
34     char a[8];
35     memset(a,0,8);
36     reqorder(ch,a,1);    
37     printf("%s\n", a);
38     return 0;
39 }



 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *str = "id1=0&id2=0&id3=%E4%BA%8C%E4%BB%BD&id4=1c&&_=1541475472570";
    
    int i,n,m;
    char *p=str;
    char p1[32]={0};//不能用char *p1=NULL;
    char p2[32]={0};//不能用char *p2=NULL;
 
    for(n=0,i=0;i<strlen(p);i++)
    {
        if(*(p+i)=='=')
        {
            if(n==2)
            {
                i++;
                for(m=0;*(p+i)!='&';i++,m++)
                {
                    *(p1+m)=*(p+i);
                }
                printf("%s\n",p1);
            }
            if(n==3)
            {
                i++;
                for(m=0;*(p+i)!='&';i++,m++)
                {
                    *(p2+m)=*(p+i);
                }
                printf("%s\n",p2);
            }
            n++;
        }
    }
     
    return 0;
}

运行结果

 

 

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

char *reporder(char *input, int n)
{
    printf("enter reporder function\n");

    int i = 0;
    static char output[32] = "\0";

    if(n==1)
    {
        while (*(input + i) != ' ')
        {
            *(output + i) = *(input + i);
            i++;
        }
    }

    return output;
}

void main()
{
    char str[32]="hello 1 world 2";
    
    printf("str=%s\n",reporder(str, 1));

    return;
}

 

posted @ 2017-05-19 18:18  quliuliu2013  阅读(8351)  评论(0编辑  收藏  举报