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;
}

運行結果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM