欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串操作函数解析

程序员文章站 2024-02-17 16:27:04
...

        在编程的时候我们经常会用到一些库函数来操作字符串,例如strcmp,strlen,strcat,  strstr, strcpy,  strchar ,  memcpy,  memmov,  memset     接下来我们就看一看这些函数。

        1 strcmp:字符串操作函数解析

字符串操作函数解析

          从msdn的解释中可以看出字符串比较这个函数有两个参数string1和string2,而且这俩参数都是常量字符串(const修饰)。返回值是一个int型。当string1小于string2的时候会返回一个小于0的数,当string1等于string2的时候会返回0,当string1小于string2的时候会返回一个大于0的数。我们可以根据返回值来判断这两个字符串的关系。

例如 :  

#define _CRT_SECURE_NO_DEPRECATE 1
#include <stdio.h>    
#include <stdlib.h>   
#include <string.h>
int main()
{
	char str1[]="hello future";
	char str2[]="hello my future";
	int ret;
	ret = strcmp(str1,str2);
	if(ret>0)
		printf("str1大于str2");
	else if(ret=0)
		printf("str1等于str2");
	else
		printf("str1小于str2");
	system("pause");
    return 0;
}
字符串操作函数解析

    2 strlen: 字符串操作函数解析

字符串操作函数解析

      从msdn的解释中可以看出这个函数有一个参数 const char *string,  返回值是一个size_t(无符号整形),因为字符串的个数根本就不可能是一个负数。

我们来看看msdn给的例子:

Example

/*  STRLEN.C */

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

void main( void )
{
   char buffer[61] = "How long am I?";
   int  len;
   len = strlen( buffer );
   printf( "'%s' is %d characters long\n", buffer, len );
}


Output

'How long am I?' is 14 characters long


   3  strcat:

字符串操作函数解析

字符串操作函数解析

     strcat函数实现的功能是把一个字符串连接到另一个字符串的后面。既然是两个字符串连接就要有两个参数即char *strDestination和const char *strSource; 返回值是一个char* 指针,指向strDestination的首元素地址,这样有利于作为链式访问使用。

#define _CRT_SECURE_NO_DEPRECATE 1
#include <stdio.h>    
#include <stdlib.h>   
#include <string.h>
int main()
{
	char str1[]="Do you want to ";
        char str2[]="play baketball?";
	printf("%s  \n",strcat(str1,str2));//返回值char * 作为首元素用于实现链式访问
	system("pause");
        return 0;
}
字符串操作函数解析
      当我们使用strcat的时候有一点要注意,不能使用strcat(str1,str1)即給一个字符串自身连接自己本身。否则会出现如下情况。

字符串操作函数解析

如果非要自身连接自身请使用strncat.

      4 strstr:

字符串操作函数解析

字符串操作函数解析

这个函数的功能是在一个字符串里面寻找另一个字符串是否存在。这个函数有两个参数即const char *string ,const char *strcharset,返回值是一个char *指针。函数返回指向字符串中第一次出现strcharset,或null如果strcharset不出现在字符串。如果strcharset指向一个字符串的长度为零,函数返回字符串。

我们来看一个msdn中的例子 便于理解

Example

/* STRSTR.C */

#include <string.h>
#include <stdio.h>

char str[] =    "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

void main( void )
{
   char *pdest;
   int  result;
   printf( "String to be searched:\n\t%s\n", string );
   printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
   pdest = strstr( string, str );
   result = pdest - string + 1;
   if( pdest != NULL )
      printf( "%s found at position %d\n\n", str, result );
   else
      printf( "%s not found\n", str );
}


Output

String to be searched:
   The quick brown dog jumps over the lazy fox
            1         2         3         4         5
   12345678901234567890123456789012345678901234567890

lazy found at position 36


   5 strcpy:

字符串操作函数解析

字符串操作函数解析

这个函数实现的是字符串拷贝功能。有两个参数 char *strDestination 和 const char *strSource。从这俩参数我们就可以直接看出是把const char *strSource中的字符串拷贝到char *strDestination中因为是改变char *strDestination中的内容所以要传址。而使用const的修饰char *strSource使之成为常量字符串是为了增加程序的健壮性(便于记忆俩参数的用处,放置俩参数放错引起的不必要的错误)。而返回值char * 指向拷贝完成的字符串首元素。便于链式访问。

看看msdn的例子

Example

/* STRCPY.C: This program uses strcpy
 * and strcat to build a phrase.
 */

#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[80];
   strcpy( string, "Hello world from " );
   strcat( string, "strcpy " );
   strcat( string, "and " );
   strcat( string, "strcat!" );
   printf( "String = %s\n", string );
}


Output

String = Hello world from strcpy and strcat!

  6 strchr:

字符串操作函数解析字符串操作函数解析

        函数功能:在一个字符串里面寻找一个字符。这个函数有两个参数 const修饰的常量字符串char *string,和int型变量c。c是用来定位这个字符在常量字符串string中的位置的。 char *指向的是要找的这个字符第一次出现在常量字符串string中的位置(可能存在多个该字符)。如果不存在则表示不存在。

看看例子

Example

/* STRCHR.C: This program illustrates searching for a character
 * with strchr (search forward) or strrchr (search backward).
 */

#include <string.h>
#include <stdio.h>

int  ch = 'r';

char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

void main( void )
{
   char *pdest;
   int result;

   printf( "String to be searched: \n\t\t%s\n", string );
   printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
   printf( "Search char:\t%c\n", ch );

   /* Search forward. */
   pdest = strchr( string, ch );
   result = pdest - string + 1;
   if( pdest != NULL )
      printf( "Result:\tfirst %c found at position %d\n\n", 
              ch, result );
   else
      printf( "Result:\t%c not found\n" );

   /* Search backward. */
   pdest = strrchr( string, ch );
   result = pdest - string + 1;
   if( pdest != NULL )
      printf( "Result:\tlast %c found at position %d\n\n", ch, result );
   else
      printf( "Result:\t%c not found\n" );
}


Output

String to be searched: 
      The quick brown dog jumps over the lazy fox
               1         2         3         4         5
      12345678901234567890123456789012345678901234567890

Search char:   r
Result:   first r found at position 12

Result:   last r found at position 30


   7 memcpy:

字符串操作函数解析字符串操作函数解析
功能:从缓存中拷贝字符。 参数 void  *dest  (需要拷贝到的目的地) const void *src(拷贝源)

size_t  (需要拷贝的个数)  返回值  void *  (memcpy returns the value of dest.

例子:msdn给的这个例子  很长   但是实现的功能确实非常简单的,看一眼就能明白。

Example

/* MEMCPY.C: Illustrate overlapping copy: memmove
 * handles it correctly; memcpy does not.
 */

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
 *                  12345678901234567890123456789012345678901234567890
 */

void main( void )
{
   printf( "Function:\tmemcpy without overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 40 );
   printf( "Destination:\t%s\n", string1 + 16 );
   memcpy( string1 + 16, string1 + 40, 3 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );

   /* Restore string1 to original contents */
   memcpy( string1 + 16, string2 + 40, 3 );

   printf( "Function:\tmemmove with overlap\n" );
   printf( "Source:\t\t%s\n", string2 + 4 );
   printf( "Destination:\t%s\n", string2 + 10 );
   memmove( string2 + 10, string2 + 4, 40 );
   printf( "Result:\t\t%s\n", string2 );
   printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );

   printf( "Function:\tmemcpy with overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 4 );
   printf( "Destination:\t%s\n", string1 + 10 );
   memcpy( string1 + 10, string1 + 4, 40 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}


Output

Function:   memcpy without overlap
Source:      fox
Destination:   dog jumps over the lazy fox
Result:      The quick brown fox jumps over the lazy fox
Length:      43 characters

Function:   memmove with overlap
Source:      quick brown fox jumps over the lazy dog
Destination:   brown fox jumps over the lazy dog
Result:      The quick quick brown fox jumps over the lazy dog
Length:      49 characters

Function:   memcpy with overlap
Source:      quick brown dog jumps over the lazy fox
Destination:   brown dog jumps over the lazy fox
Result:      The quick quick brown dog jumps over the lazy fox
Length:      49 characters


    8  memmov:

字符串操作函数解析字符串操作函数解析
函数功能:缓冲区数据移动。 参数 void  *dest  (需要移动到的目的地) const void *src(需要移动的数据源)  size_t count (需要拷贝的类型字节数)  返回值  void *  (memmove returns the value of dest.

例子:

Example

/* MEMCPY.C: Illustrate overlapping copy: memmove
 * handles it correctly; memcpy does not.
 */

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
 *                  12345678901234567890123456789012345678901234567890
 */

void main( void )
{
   printf( "Function:\tmemcpy without overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 40 );
   printf( "Destination:\t%s\n", string1 + 16 );
   memcpy( string1 + 16, string1 + 40, 3 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );

   /* Restore string1 to original contents */
   memcpy( string1 + 16, string2 + 40, 3 );

   printf( "Function:\tmemmove with overlap\n" );
   printf( "Source:\t\t%s\n", string2 + 4 );
   printf( "Destination:\t%s\n", string2 + 10 );
   memmove( string2 + 10, string2 + 4, 40 );
   printf( "Result:\t\t%s\n", string2 );
   printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );

   printf( "Function:\tmemcpy with overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 4 );
   printf( "Destination:\t%s\n", string1 + 10 );
   memcpy( string1 + 10, string1 + 4, 40 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}

Output

Function:   memcpy without overlap
Source:      fox
Destination:   dog jumps over the lazy fox
Result:      The quick brown fox jumps over the lazy fox
Length:      43 characters

Function:   memmove with overlap
Source:      quick brown fox jumps over the lazy dog
Destination:   brown fox jumps over the lazy dog
Result:      The quick quick brown fox jumps over the lazy dog
Length:      49 characters

Function:   memcpy with overlap
Source:      quick brown dog jumps over the lazy fox
Destination:   brown dog jumps over the lazy fox
Result:      The quick quick brown dog jumps over the lazy fox
Length:      49 characters
 

    9   memset  :

字符串操作函数解析字符串操作函数解析

功能:把指定的缓存空间设置为指定的字符。    参数         void  *dest  (需要设置的目的地)                          c:需要设置的字符类型                 size_t count (需要设置的指定类型的个数)                   返回值  void *  (memmove returns the value of dest.

例子:比起之前几个的繁琐相当精简

Example

/* MEMSET.C: This program uses memset to
 * set the first four bytes of buffer to "*".
 */

#include <memory.h>
#include <stdio.h>

void main( void )
{
   char buffer[] = "This is a test of the memset function";

   printf( "Before: %s\n", buffer );
   memset( buffer, '*', 4 );
   printf( "After:  %s\n", buffer );
}

Output

Before: This is a test of the memset function
After:  **** is a test of the memset function
在使用这些字符串操作函数的时候我们需要注意的地方是 :

    1 各个参数的意义,我们可以通过const修饰减少一些不必要的错误引起的失。

    2  巧妙的利用返回值,使程序更加的巧妙。