本次要介绍的函数有:
- strlen()
- strcpy()
- strcmp()
- strcat()
- strncpy()
- strncmp()
- strncat()
- strerror()
- islower()
- isupper()
- tolower()
- toupper()
- memmove()
- memcpy()
- memset()
- memcmp()
- strstr()
- strtok()
分类:求字符串长度函数:1
长度不受限制的字符串函数:2,3,4
长度受限制的字符串函数:5,6,7
字符串查找函数:17,18
错误信息报告函数:8
字符操作函数:9,10,11,12
内存操作操作函数:13,14,15,16
!建议有些C语言基础的博友观读!
声明 (1)
size_t strlen (construction char * str)
代码:
int main()
{
char ch[]=“zhansan”;
size_t a = strlen(ch) ;
printf("%d\n", a);
// 或者
size_t b = strlen("hello world!");
printf("%d\n", b) ;
return 0 ;
}
声明 (2)
char* strcpy (char* dest, char* sour)
代码:
int main()
{
char ch1[10]=“”;
char ch2[]= “abcdefg”;
strcpy(ch1 ,ch2);
printf("%s\n", ch1);
return 0 ;
}
//接下来我们来模拟strcpy 函数—>my_strcpy()
函数声明:
char* my_strcpy(char*dest,
const char* sour);
代码块
char*my_strcpy (char*dest ,
const char*sour)
{
assert(dest&&sour);
char* ret = dest ;
while(*dest++ == *sour++)
{
;
}
return ret ;
}
int main()
{
char a[10] = " ";
char b [] = "abcdeks";
char * ret = my_strcpy(a, b);
printf("%s\n", ret);
// 或者打印字符串这样写:
printf(ret);
return 0;
}
声明(3)
int strcmp(char*dest , const char* sour)
代码:
int main()
{
char ch[] = "abcdshdh";
char ch1[] = "abcrg";
int ret = strcmp(ch, ch1);
if(ret ==0)
{
printf("字符串ch=ch1\n");
}
else if (ret > 0)
{
printf("字符串ch>ch1\n");
}
else
{
printf("字符串ch<ch1\n");
}
return 0 ;
}
//模拟实现strcmp函数
—>my_strcmp()
函数声明:
int my_strcmp(char* dest,const char* sour)
代码块
int my_strcmp(char* dest,const char* sour)
{
assert(dest&&sour);
while(*dest ==*sour)
{
if(*sour==’\0 ’)
{
break;
}
dest++;
sour++;
}
return *dest - *sour ;
}
int main()
{
char ch[] = "abcdshdh";
char ch1[] = "abcrg";
int ret = my_strcmp(ch, ch1);
if(ret ==0)
{
printf("字符串ch=ch1\n");
}
else if (ret > 0)
{
printf("字符串ch>ch1\n");
}
else
{
printf("字符串ch<ch1\n");
}
return 0 ;
}
声明(4)
char * strcat (char * dset,
const char *sour);
代码:
int main ()
{
char ch3[20]=“dhshg”;
char * ret = strcat( ch3,“haoxue”);
printf(ret);
return 0 ;
}
//模拟实现strcat函数
—>my_strcat()
函数声明:
char* my_strcat (char*dest ,
const char*sour);
代码块
char* my_strcat (char*dest,
const char*sour)
{
assert(dest&&sour);
char* ret = dest;
while(*dest)
{
dest++;
}
while(*dest++ = *sour++)
{
;
}
return ret ;
}
int main()
{
char arr[20] = “shsvsba”;
char arr1[] =“wangzhe”
char * ret= my_strcat(arr , arr1);
printf(ret);
return 0 ;
}
声明(5)
char*strncpy(char*dest,const char* sour, size_t num)
代码:
int main()
{
char arr1[20] = “zhang”;
char arr2[] = “san feng”;
char* ret = strncpy(arr1 , arr2 , 3);
printf(arr1);
return 0 ;
}
//模拟实现函数strncpy
—>my_strncpy
函数声明
char* my_strncpy(char*dest,const*sour,
size_t num);
代码块
char* my_strncpy(char*dest,const*sour,
size_t num)
{
assert(dest&&sour);
char*ret = dest ;
while(num--)
{
if(*sour==’\0’)
{
*dest = *sour ;
break;
}
*dest++ =*sour++;
}
return ret ;
}
int main()
{
char arr1[20] = “lisi”;
char arr2[] = “yu sanfeng”;
char* ret =my_ strncpy(arr1 , arr2 , 6); printf(arr1);
return 0 ;
}
声明(6)
int strncmp(char*dest,const char* sour ,
size_t num);
代码:
int main()
{
char ch1[] = “abcwoiks”;
char ch2[] = “abcwikks”;
int ret = strncmp(ch1 , ch2 , 5);
if(ret ==0)
{
printf("字符串相等\n");
}
else if (ret > 0)
{
printf("字符串大于\n");
}
else
{
printf("字符串小于\n");
}
return 0 ;
}
//模拟实现函数strncmp
—>my_strncmp()
函数声明
int my_strncmp(char*dest,const char* sour ,size_t num);
代码块
int my_strncmp(char*dest,const char* sour ,size_t num)
{
assert(dest&&sour) ;
while(num--)
{
if(*dest != *sour ||( *dest ==*sour && *sour== ’\0’))
{
break;
}
}
return *dest - *sour ;
}
int main()
{
char ch1[] = “abcwoiks”;
char ch2[] = “abcwikks”;
int ret = my_strncmp(ch1 , ch2 , 5);
if(ret ==0)
{
printf("字符串相等\n");
}
else if (ret > 0)
{
printf("字符串大于\n");
}
else
{
printf("字符串小于\n");
}
return 0;
}
声明(7)
char* strncat (char* dest,const char*sour,
size_t num);
代码:
int main()
{
char ch4[20] =“hello world”;
char arr[] = “pengyou”;
char* tre = strncat (ch4 ,arr ,8);
printf(tre);
return 0 :
}
//模拟实现函数strncat
—>my_strncat()
函数声明
char*my_strncat(char*dest, const char * sour,
size_t num);
代码块
char*my_strncat(char*dest,
const char*sour,
size_t num);
{
assert(dest&&sour);
char* ret = dest ;
while(*dest)
{
dest++ ;
}
while(num--)
{
if(*sour==’\0’)
{
*dest = *sour ;
break;
}
*dest++ =*sour++;
}
*dest = ’\0’;
return ret ;
}
声明(17)
char* strstr (char*dest, const char* sour);
代码:
int main()
{
char ch1[15] = “fhsjjebzhshs”;
char arr[] = “jeb”;
char* ret = strstr(ch1 , arr);
if( ret != NULL)
{
printf(ret);
}
else
{
printf("找不到\n");
}
return 0 ;
}
//模拟实现函数strstr
—>my_strstr
函数声明:
char* my_strstr (char*dest,
const char* sour)
代码
char* my_strstr (char*dest,
const char* sour)
{
assert(dest&&sour);
char*st1=NULL;
char*st2 = NULL;
char*p = dest;
while(*p)
{
st1 = p ;
st2 = sour ;
while(*st1==*st2 && *st2 !=’\0’)
{
st1++;
st2++;
}
if(*st2== ’\0’)
{
return p ;
}
p++;
}
return NULL ;
}
int main()
{
char ch1[15] = “fhsjjebzhshs”;
char arr[] = “bzhsh”;
char* ret = my_strstr(ch1 , arr);
if( ret != NULL)
{
printf(ret);
}
else
{
printf("找不到\n");
}
return 0 ;
}
声明 (18)
char * strtok (char* dest ,const char* cep);
代码:
int main()
{
char ch[20] = “wang@yang,ming”;
char ch1[20] = “”;
char arr[] = “@,”;
strcpy(ch1 , ch );
char * ret = strtok (ch1 , arr );
if ( ret != ’\0’)
{
printf(ret);
}
ret = strtok (NULL, arr );
if ( ret != ’\0’)
{
printf(ret);
}
ret = strtok (NULL , arr );
if ( ret != ’\0’)
{
printf(ret);
}
//或者这样
char * ret = NULL;
for(ret = strtok (ch1,arr) ; ret !=NULL; ret = strtok(NULL,arr))
{
printf(ret) ;
}
return 0 ;
}
声明(8)
char * strerror ( int errnum );
代码:
int main()
{
FILE* ret = fopen(“tect.cr” , “r”) ;
if (ret ==NULL )
{
printf("%s\n", strerror(errno));
}
return 0 ;
}
//函数fopen() 的声明
FILE * fopen ( const char * filename,
const char * mode);
声明(9)
int islower ( int c );
代码:
int main()
{
int n = ’w ’;
// 或者 char ret = 0;
int n = (int)scanf("%c", &ret);\\
if (islower(n))
{
printf ("是小写字母\n");
}
else
{
printf("不是小写字母\n");
}
return 0 ;
}
声明(10)
int isupper ( int c );
代码:
int main()
{
int n = ’W ’;
// 或者 char ret = 0;
int n = (int)scanf("%c", &ret);\\
if (isupper(n))
{
printf ("是大写字母\n");
}
else
{
printf("不是大写字母\n");
}
return 0 ;
}
声明(11)(12)
int toupoer ( int c );
int tolower ( int c );
代码:
int main()
{
printf("%c\n", tolower(’Z ’));
printf("%c\n", toupper(’x ’));
//一个为大写字母转小写字母
// 一个为小写字母转大写字母
// 若()中是其他字符,结果原样打印;
return 0 ;
}
声明(13)
void * memmove ( void * dest,
const void * sour
size_t num);
代码:
int main()
{
int arr [10] = {1,2,3,5,6,9,5,6,9,5};
memmove(arr , arr+3 , 20);
int i = 0 ;
for (i = 0; i<10;i++ )
{
printf("%d ", arr[i]);
} //打印改变后整型数组;
return 0 ;
}
//模拟实现函数memmove
—>my_memmove
void * my_memmove ( void * dest,const void * sour, size_t num );
代码
void *my_memmove ( void * dest, const void * sour, size_t num ){
assert (dest&&sour);
void * ret = dest ;
if(dest > sour)
{
while (num--)
{
*((char*)dest + num )
= *((char*)sour + num);
}
}
else
{
while (num--)
{
*(char*)dest = *(char*) sour ;
dest = (char*)dest +1;
sour = (char*)sour +1 ;
}
}
return ret ;
}
int main()
{
int arr [10] = {1,2,3,5,6,9,5,6,9,5}; my_memmove(arr , arr+3 , 20 );
int i = 0 ;
for (i = 0; i<10;i++ )
{
printf("%d ", arr[i]);
}
return 0 ;
}
声明(14)
void * memcpy ( void * dest,
const void * sour
size_t num);
代码:
int main()
{
int arr [10] = {1,2,3,5,6,9,5,6,9,5};
int are2[]= {2,4,7,9,3,2,6,7};
memcpy(arr , arr3, 24 );
int i = 0 ;
for (i = 0; i<10;i++ )
{
printf("%d ", arr[i]);
} //打印改变后整型数组;
return 0 ;
}
//模拟实现memcpy
—>my_memcpy()
函数声明
void * my_memcpy ( void * dest,
const void * sour
size_t num);
代码块
void *my_memcpy ( void * dest,
const void * sour
size_t num);
{
assert (dest&&sour);
void* ret = dest ;
while (num--)
{
*(char*)dest = *(char*) sour ;
dest = (char*)dest +1;
sour = (char*)sour +1 ;
}
return ret ;
}
int main()
{
int arr [10] = {1,2,3,5,6,9,5,6,9,5};
int are2[]= {2,4,7,9,3,2,6,7};
memcpy(arr , arr3, 24 );
int i = 0 ;
for (i = 0; i<10;i++ )
{
printf("%d ", arr[i]);
}
return 0 ;
}
//C语言规定若拷贝时内存有重叠部 分,则交给memmove函数来实现;
不同编译器下memcpy函数功能不用,
有的情况下能够进行拷贝内存重叠部 分,有的情况下不能。
声明(15)
void * memset ( void * ptr, int value,
size_t num )
代码:
int main()
{
int arr[6] = {1,2,4,5,7,9};
memset(arr , 0 , 24 );
int m = 0 ;
for (m=0 ; m <6 ; m++)
{
printf("%d ", arr[m] );
}
//或者
char ch [10] =“wang zi”;
memset(ch+5 , ’x ’,2 );
printf (ch); \\
return 0 ;
}
声明(16)
int memcmp ( const void * ptr1,
const void * ptr2,
size_t num )
代码:
int main()
{
int arr[5] = {2,4,6,3,9};
int arr2[5] = {2,4,5,9,1};
int a = memcmp(arr , arr2, 4);
if(a == 0)
{
printf("相等\n");
}
else if ( a > 0)
{
printf("大于\n");
}
else
{
printf("小于\n");
}
//该函数比较时一对字节进行比较:
arr: 02 04 06 03 09
arr2 : 02 04 05 09 01
//
return 0 ;
}
内存函数可以运用于任何类型的数据;
字符、串函数只能运用字符型的数据;
运用是要注意头文件的引用;
本篇的代码符号一律为小写;
望
各位同仁
批评指正潜在的错误!!