大家好,关于c语言strstr函数用法很多朋友都还不太明白,今天小编就来为大家分享关于strstr函数详解的知识,希望对各位有所帮助!
一、C语言中,strstr函数的功能是什么不用库函数,自己写怎么写
1、函数原型是char*strstr(char*str1,char*str2);作用是找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
2、如果找到返回该位置的指针。若找不到,返回NULL指针。如果不用库函数写,就得用到串操作中的模式匹配算法,或者他的改进算法!!!
3、这里给你一个模式匹配算法不明白的,可以交流下!!
4、while(i<strlen(a)&&j<strlen(b))
5、 i=k;//可用于记录原来i的位置;
6、if(j>=strlen(b))//判断找到字符串的条件
7、 printf("未找到字符串%s\n",b);
8、c编程高手团队正在招新,有意者速速行动,一起学习,一起努力!!
二、strstr函数的用法。。。
1、strchr函数的语法格式怎么用?它的作用与strstr函数有什么区别?
2、在C语言中 strchr和 strstr函数都被包含在<string.h>头文件中,也就是要调用它们时要在程序前面包含<string.h>头文件,也就是写这个语句:#include<string.h>
3、strchr函数原型:char* strchr(char* str, int ch);功能就是找出在字符串str中第一次出项字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是 null)。
4、strstr函数原型: char* strstr(char* str1,char* str2);功能就是找出在字符串str1中第一次出项字符串str2的位置(也就是说字符串sr1中要包含有字符串str2),找到就返回该字符串位置的指针(也就是返回字符串str2在字符串str1中的地址的位置),找不到就返回空指针(就是 null)。
5、它们一个是求一个字符在字符串中得位置,另一个是求一个字符串在另一个字符串中的位置。
三、c语言string的用法大全
C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器。C语言string的用法有哪些呢,请看看下面我为你整理总结的c语言string的用法大全_C语言中string使用方法。
函数原型:char*strdup(const char*s)
函数功能:字符串拷贝,目的空间由该函数分配
函数返回:指向拷贝后的字符串指针
char*dup_str,*string="abcde";
函数原型:char* strcpy(char* str1,char* str2);
函数功能:把str2指向的字符串拷贝到str1中去
函数返回:返回str1,即指向str1的指针
printf("thestringis:%s\n",string);
函数原型:char*strncpy(char*dest, const char*src,intcount)
函数功能:将字符串src中的count个字符拷贝到字符串dest中去
参数说明:dest-目的字符串,src-源字符串,count-拷贝的字符个数
char*src="bbbbbbbbbbbbbbbbbbbb";//20'b's
chardest[50]="aaaaaaaaaaaaaaaaaaaa";//20'a's
/*******************************************
*******************************************/
注意:strncpy只复制指定长度的字符,不会自动在末尾加'\0'。若指定长度超过源字符串长度,不够的部分补‘\0’,
函数原型:char* strcat(char* str1,char* str2);
函数功能:把字符串str2接到str1后面,str1最后的'\0'被取消
函数原型:char*strncat(char*dest, const char*src, size_t maxlen)
函数功能:将字符串src中前maxlen个字符连接到dest中
strncat(buffer,"*************",4);
注意:与strncpy不同的是,strncat会自动在末尾加‘\0’,若指定长度超过源字符串长度,则只复制源字符串长度即停止
函数原型:int strcmp(char* str1,char* str2);
函数功能:比较两个字符串str1,str2.
函数返回:str1<str2,返回负数;str1=str2,返回 0;str1>str2,返回正数.
char*buf1="aaa",*buf2="bbb",*buf3="ccc";
printf("buffer2isgreaterthanbuffer1\n");
printf("buffer2islessthanbuffer1\n");
printf("buffer2isgreaterthanbuffer3\n");
printf("buffer2islessthanbuffer3\n");
函数原型:int strncmp(char*str1,char*str2,int count)
函数功能:对str1和str2中的前count个字符按字典顺序比较
函数返回:小于0:str1<str2,等于0:str1=str2,大于0:str1>str2
参数说明:str1,str2-待比较的字符串,count-比较的长度
//为使测试程序更简练,此处假定了strncmp只返回-1,0,1三个数
charres_info[]={'<','=','>'};
printf("1:str1%cstr2\n",res_info[res+1]);
printf("3:str1%cstr2\n",res_info[res+1]);
/****************************************
*****************************************/
函数原型:char*strpbrk(const char*s1, const char*s2)
函数功能:得到s1中第一个“同时也出现在s2中”字符的位置指针
/**************************************
**************************************/
函数原型:int strcspn(const char*s1, const char*s2)
函数功能:统计s1中从头开始直到第一个“来自s2中的字符”出现的长度
printf("%d\n",strcspn("abcbcadef","cba"));
printf("%d\n",strcspn("xxxbcadef","cba"));
printf("%d\n",strcspn("123456789","cba"));
函数原型:int strspn(const char*s1, const char*s2)
函数功能:统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度
printf("%d\n",strspn("abcbcadef","cba"));
printf("%d\n",strspn("xxxbcadef","cba"));
printf("%d\n",strspn("123456789","cba"));
函数原型:char* strchr(char* str,char ch);
函数功能:找出str指向的字符串中第一次出现字符ch的位置
函数返回:返回指向该位置的指针,如找不到,则返回空指针
参数说明:str-待搜索的字符串,ch-查找的字符
printf("%cisthe%dcharacterof\"%s\"\n",ch,(int)(p-str+1),str);
printf("PressESCtoquit!\n\n");
/********************************************
iisthe3characterof"Thisisastring!"
sisthe4characterof"Thisisastring!"
**********************************************/
函数原型:char*strrchr(const char*s, int c)
函数功能:得到字符串s中最后一个含有c字符的位置指针
strcpy(string,"Thisisastring");
printf("Thecharacter%cisatposition:%d",c,ptr-string);
printf("Thecharacterwasnotfound");
函数原型:char* strstr(char* str1,char* str2);
函数功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
函数返回:返回该位置的指针,如找不到,返回空指针
char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;
printf("Thesubstringis:%s\n",ptr);
函数功能:将字符串中的所有字符颠倒次序排列
charforward[]="string";//原文中定义为char*是不对的,指向代码段的指针内容是不可变的
printf("Beforestrrev():%s",forward);
printf("Afterstrrev():%s",forward);
/************************************
************************************/
函数原型:char*strnset(char*s, int ch, size_t n)
函数功能:将字符串s中前n个字符设置为ch的值
charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";
printf("stringbeforestrnset:%s\n",string);
printf("stringafterstrnset:%s\n",string);
/*************************************************
stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa
stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa
*************************************************/
函数原型:char*strset(char*s, int ch)
函数功能:将字符串s中所有字符设置为ch的值
printf("Beforestrset():%s",string);
printf("Afterstrset():%s",string);
函数原型:char*strtok(char*s1, const char*s2)
函数功能:分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词)
函数返回:字符串s1中首次出现s2中的字符前的子字符串指针
参数说明:s2一般设置为s1中的分隔字符
规定进行子调用时(即分割s1的第二、三及后续子串)第一参数必须是NULL
在每一次匹配成功后,将s1中分割出的子串位置替换为NULL(摘下链中第一个环),因此s1被破坏了
函数会记忆指针位置以供下一次调用
buffer=strdup("Findwords,allofthem.");
}//根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔
PS:根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔
函数功能:将字符串s中的字符变为大写
charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//会影响原字符串的内存,用char[]来声明
函数功能:将字符串中的字符变为小写字符
函数原型:char*strerror(int errnum)
函数功能:得到错误信息的内容信息
函数返回:错误提示信息字符串指针
函数原型:void*memcpy(void*dest, const void*src, size_t n)
参数说明:src-源字符串,n-拷贝的最大长度
所属文件:<string.h>,<mem.h>
charsrc[]="******************************";
chardest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
printf("destinationbeforememcpy:%s\n",dest);
ptr=memcpy(dest,src,strlen(src));
printf("destinationaftermemcpy:%s\n",dest);
/*************************************************************
destinationbeforememcpy:abcdefghijlkmnopqrstuvwxyz0123456709
destinationaftermemcpy:******************************456709
**************************************************************/
函数原型:void*memccpy(void*dest, const void*src, int c, size_t n)
函数功能:字符串拷贝,到指定长度或遇到指定字符时停止拷贝
参数说明:src-源字符串指针,c-中止拷贝检查字符,n-长度,dest-拷贝底目的字符串指针
所属文件:<string.h>,<mem.h>
char*src="Thisisthesourcestring";
ptr=memccpy(dest,src,'c',strlen(src));
printf("Thecharacterwasfound:%s",dest);
printf("Thecharacterwasn'tfound");
/*****************************************
Thecharacterwasfound:Thisisthesourc
*****************************************/
PS:指定字符被复制到dest中,memccpy返回了dest中指定字符的下一处的地址,返回NULL表示未遇到指定字符
函数原型:void*memchr(const void*s, int c, size_t n)
函数功能:在字符串中第开始n个字符中寻找某个字符c的位置
函数返回:返回c的位置指针,返回NULL时表示未找到
参数说明:s-要搜索的字符串,c-要寻找的字符,n-指定长度
所属文件:<string.h>,<mem.h>
ptr=memchr(str,'r',strlen(str));
printf("Thecharacter'r'isatposition:%d",ptr-str);
printf("Thecharacterwasnotfound");
函数原型:int memcmp(const void*s1, const void*s2,size_t n)
函数功能:按字典顺序比较两个串s1和s2的前n个字节
函数返回:<0,=0,>0分别表示s1<,=,>s2
参数说明:s1,s2-要比较的字符串,n-比较的长度
所属文件:<string.h>,<mem.h>
printf("Thestringstoposition5are");
函数原型:int memicmp(const void*s1, const void*s2, size_t n)
函数功能:按字典顺序、不考虑字母大小写对字符串s1,s2前n个字符比较
函数返回:<0,=0,>0分别表示s1<,=,>s2
参数说明:s1,s2-要比较的字符串,n-比较的长度
所属文件:<string.h>,<mem.h>
printf("Thestringstoposition5are");
/**************************************
Thestringstoposition5arethesame
***************************************/
函数原型:void*memmove(void*dest, const void*src, size_t n)
参数说明:src-源字符串,n-拷贝的最大长度
所属文件:<string.h>,<mem.h>
chardest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
printf("destinationpriortomemmove:%s\n",dest);
printf("destinationaftermemmove:%s",dest);
PS:与memcpy不同的是,memmove可以处理目的字符串与源字符串地址空间出现重叠的情况,可保证待复制的内容不被破坏。
函数原型: void*memset(void*s, int c, size_t n)
函数功能:字符串中的n个字节内容设置为c
参数说明: s-要设置的字符串,c-设置的内容,n-长度
所属文件:<string.h>,<mem.h>
printf("Bufferbeforememset:%s/n",buffer);
memset(buffer,'*',strlen(buffer)-1);
printf("Bufferaftermemset:%s",buffer);
c语言string的用法大全相关文章:
★ Linux C语言字符与字符串处理
关于c语言strstr函数用法到此分享完毕,希望能帮助到您。