C测试小程序

C测试小程序

1、                 字符串类

1.1        strstr

功能:查找和获取子串

void test_strstr()

{

        char *str="Borland   International";

        char *str1="B",*ptr1;

        char *str2="na",*ptr2;

        char *str3="",*ptr3;

            

        ptr1=strstr(str,str1);    

        printf("The   substring1   is:   %s/n",   ptr1);//Borland   International

          

         ptr2=strstr(str,str2);    

        printf("The   substring2   is:   %s/n",   ptr2);//national

 

         ptr3=strstr(str,str3);    

        printf("The   substring3   is:   %s/n",   ptr3); //Borland   International

}   

/*****************************************************

  char *strstr( const char *string, const char *strCharSet );

  Each of these functions returns a pointer to the first occurrence of strCharSet in string,

  or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length,

  the function returns string.

  ******************************************************/

1.2   atoi_itoa

功能:charint之间的互相转换

void char2int_1()

{

char* temp1="123";

int a1=atoi(temp1);

printf("char2int_1()/ta1+1=%d/n",a1+1);//124

}

void int2char_1()

{

      char temp[20];

      char* retp;

      int a2=65;

      retp=_itoa(a2,temp,16);

      printf("int2char_1/tretp=%s/n",retp);//41

      printf("int2char_1/ttemp=%s/n",temp);//41

      retp=_itoa(a2,temp,10);

      printf("int2char_1/tretp=%s/n",retp);//65

      printf("int2char_1/ttemp=%s/n",temp);//65

}

void int2char_2()

{

      char temp[20];

      int a2=65;

    sprintf(temp,"%d",65);

printf("int2char_2/ttemp=%s/n",temp);//65

}

1.3      nospace

功能:去除字符串两边的空格

char* noleftspace(char* str)

{

      char* temp;

      printf("before noleftspace=%s/n",str);

    while(*str!='/0')

      {

           if(*str==' ')

           {

                 str=str+1;

               temp=str;

           }

           else

           {

                 temp=str;

                 break;

           }

      }

      printf("after noleftspace=%s/n",temp);

      return temp;

}

 

char* norightspace(char* str)

{

 

      int length=strlen(str);

      int a=length-1;

      char* temp;

      *str='1';

           printf("before noleftspace=%s/n",str);

      while(a>0)

      {

           if(*(str+a)!=' ')

           {

                 (char*)str;

                 *(str+a+1)='/0';

                 break;

           }

           else

                 a--;

      }

      printf("after noleftspace=%s/n",str);

      return str;

}

1.4     strchr

功能:查找字符串中某个字符出现的第一个位置

int test_strchr(void) //查找字符串中第一个匹配的字符的位置

 {

    char string[15];

    char *ptr, c = 'r';

    strcpy(string, "This is a string");

    ptr = strchr(string, c);

    if (ptr)

       printf("The character %c is at position: %d/n", c, ptr-string); //12

    else

       printf("The character was not found/n");

      printf("ptr=%s/n",ptr);//ring

    return 0;

 }

1.5     strcspn

功能:在string中,第一次出现strCharSet中任意一个字符的位置

/*******************************************************

size_t strcspn( const char *string, const char *strCharSet );

string中,第一次出现strCharSet中任意一个字符的位置

*********************************************************/

int test_strcspn(void)

 {

    char *string = "D23557890"; //string1中的D出现在string的第0个位置

    char *string1 = "7DC8";

    int length1;

 

char *string2 = "45"; //string2中的5出现在string的第3个位置

    int length2;

 

    length1 = strcspn(string, string1);

    printf("Character where strings intersect is at position %d/n", length1); //5

 

      length2 = strcspn(string, string2);

    printf("Character where strings intersect is at position %d/n", length2); //3

    return 0;

 }

1.6     strpbrk

功能:查找在string中出现的来自strCharSet第一个字符,并返回指向它的指针

/**************************************************************

char *strpbrk( const char *string, const char *strCharSet );

查找在string中出现的来自strCharSet第一个字符,并返回指向它的指针

Each of these functions returns a pointer to the first occurrence of any character

from strCharSet in string, or a NULL pointer if the two string arguments have no characters in common.

****************************************************************/

int test_strpbrk(void) //查找string2中字符在string1中出现的位置

{

   char *string1 = "abcdefghi3jklmnopqrstuvwxyz";

   char *string2 = "9876543210";

   char *ptr;

 

   ptr = strpbrk(string1, string2);

 

   if (ptr)

   {

      printf("strpbrk found first character: %c/n", *ptr); //3

        printf("the position of ptr is %d",(ptr-string1));  //9

      }

   else

      printf("strpbrk didn't find character in set/n");

 

   return 0;

}

1.7     strspn

功能:string中第一个不在strCharSet中的字符的位置

/***********************************************************

size_t strspn( const char *string, const char *strCharSet );

string中第一个不在strCharSet中的字符的位置

  ***********************************************************/

int test_strspn(void)

{

   char *string1 = "1234567890"; //5不在string2

   char *string2 = "000000213985fDC84";

   int length;

 

   length = strspn(string1, string2);

   printf("Character where strings differ is at position %d/n", length); //5

   return 0;

}

1.8     strtok

功能:拆分字符串

char string[] = "10.8.'2.173--xense rver3";

char seps[]   = "'- '";

char *token;

 

void main( void )

{

   printf( "%s/n/nTokens:/n", string );

   token = strtok( string, seps );

   while( token != NULL )

   {

      printf( " %s/n", token );

      token = strtok( NULL, seps );

   }

}

/***********************************************

10.8.

2.173

xense

rver3

************************************************/

1.9     access

功能:判断一个文件是否存在

printf ("%d",access("111.txt",0));//存在

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值