C語言提取字串

來源:互聯網
上載者:User

標籤:atoi   情況   標頭檔   字串   str   log   get   +=   檔案   

在只給<stdio.h>和<string>標頭檔的情況下,從下文中提取UserId=123456

https://www.cnblogs.com/ysjd UserId=123456 passwd=513920

代碼如下:

 1 #include<stdio.h> 2 #include<string.h> 3  4 int myatoi(char str[]) 5 { 6     int len=strlen(str); 7     char *p=str+len-1; 8     int sum=0,i=0,num; 9     while(p>=str)10     {11         num=*p-48;12         for(int j=0;j<i;j++)13         {14             num*=10;15         }16         i++;17         sum+=num;18         p--;19     }20     return sum;21 }22 23 int getUserId(char *p,int *n)24 {25     char *q=strstr(p,"UserId=");26     if(q==NULL)27         return 0;28     q+=7;29     char str[20];30     int i=0;31     while(*q>=‘0‘ && *q<=‘9‘)32     {33         str[i]=*q;34         i++;35         q++;36     }37     str[i]=‘\0‘;38     *n=myatoi(str);39     return 1;40 }41 int main()42 {43     char str[]="https://www.cnblogs.com/ysjd UserId=123456 passwd=513920";44     int n;45     int ret=getUserId(str,&n);46     if(ret==0)47     {48         printf("not found\n");49     }50     else51     {52         printf("UserId=%d\n",n);53     }54 }

 

C語言提取字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.