首页 > 编程知识 正文

c语言怎么取字符串中指定的位置,c语言如何查找字符串中指定字符串的位置

时间:2024-04-24 11:42:59 阅读:333465 作者:ZCEV

本文目录一览:

求标准C语言,查找某字符串在指定字节串中的位置代码。

#include stdio.h

#include stdlib.h

int FindStr(char* source, char* target)

{

int i,j;

int s_len=strlen(source);

int t_len=strlen(target);

if(t_lens_len)

{

return -1;

}

for(i=0;i=s_len-t_len;i++)

{

j=0;

int flag=1;

if(source[i]==target[j])

{

int k,p=i;

for(k=0;kt_len;k++)

{

if(source[p]==target[j])

{

p++;

j++;

continue;

}

else

{

flag=0;

break;

}

}

}

else

{

continue;

}

if(flag==1)

{

return i+1;

}

}

return -1;

}

int main(int argc, char *argv[]) 

{

    char s1[50]={""};

    char s2[50]={""};

    fflush(stdin);

    scanf("%[^n]",s1);

    fflush(stdin);

    scanf("%[^n]",s2);

    int re=FindStr(s1,s2);

    printf("%d",re);

    return 0;

}

C语言中,读取字符串中的一个位置

#includestdio.h

#includestdlib.h

#includestring.h

/**

*查找字符串首次出现的位置

*

*charneedle[]="name";

*charhaystack[]="mynameiszhangwoods";

*printf("strpospositionis:%d",strpos(haystack,needle));

*

*@paramhaystack在该字符串中进行查找

*@paramneedle要查找的字符串

*@return返回needle存在于haystack字符串起始的位置-1代表未找到

*/

intstrpos(constchar*haystack,constchar*needle)

{

intn,p=0;

if(*haystack*needle)

{

while(*haystack)

{

for(n=0;*(haystack+n)==*(needle+n);n++)

{

if(!*(needle+n+1))

{

returnp;

}

}

p++;

haystack++;

}

}

return-1;

}

intmain(void)

{

charneedle[]="name";

charhaystack[]="mynameiszhangnamewoods";

printf("data:%d",strpos(haystack,needle));

return0;

}

扩展资料

C语言读取主字符串中指定的字符串

#includestdio.h

#includestring.h

intmain()

{

charstr[100];

sscanf("2737237:348384122","%*[^:]:%[^n]",str);//使用正则表达式:从:开始读取到最后

printf("%sn",str);

return0;

}

c语言截取特定位置字符串

1.用指针,把最后一个字符后的位置赋给一个指针,输出该指针,就可以输出截取的字符了。

2.如果指针不熟悉的话,只能赋值给char型的数组,然后一个字母一个字母的输出了。(C语言,字符串的结尾是‘/n’,根据这个可以判定赋值的结尾)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。