string重点函数集合

<<string重点函数集合>>

1函数名:strcpy
功能
:拷贝一个字符串到另一个
用法:char *strcpy(char *destin, char *source);
程序例:

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

intmain(void)
{
   char string[10];
  char *str1 = "abcdefghi";

  strcpy(string,str1);
  printf("%s\n", string);
   return 0;
}

2函数名:strcat
功能
:字符串拼接函数
用法:char *strcat(char *destin, char *source);
程序例:

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

intmain(void)
{
   char destination[25];
  char *blank = " ", *c = "C++", *Borland ="Borland";

 strcpy(destination,Borland);
   strcat(destination, blank);
  strcat(destination, c);

   printf("%s\n",destination);
   return 0;
}

3函数名:strchr
功能
:在一个串中查找给定字符的第一个匹配之处\
用法:char *strchr(char *str, char c);
程序例:

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

intmain(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);
    else
      printf("The character was not found\n");
   return 0;
}

4函数名:strcmp
功能
:串比较
用法:int strcmp(char *str1, char *str2);
Asic码,str1>str2,返回值>0;两串相等,返回0
程序例:

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

intmain(void)
{
char *buf1 = "aaa", *buf2 = "bbb",*buf3 = "ccc";
int ptr;

ptr= strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer2 is greater than buffer 1\n");
else
      printf("buffer 2 is less than buffer 1\n");

ptr= strcmp(buf2, buf3);
if (ptr > 0)
      printf("buffer 2 is greater than buffer 3\n");
else
       printf("buffer 2 isless than buffer 3\n");

return0;
}

5函数名:strncmpi
功能
:将一个串中的一部分与另一个串比较,不管大小写
用法:int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:

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

intmain(void)
{
   char *buf1 = "BBB", *buf2= "bbb";
   int ptr;

ptr= strcmpi(buf2, buf1);

if(ptr > 0)
      printf("buffer 2is greater than buffer 1\n");

if(ptr < 0)
      printf("buffer 2is less than buffer 1\n");

if(ptr == 0)
      printf("buffer 2equals buffer 1\n");

   return0;
}



6函数名:strcspn

功能:顺序在字符串s1中搜寻与s2中字符的第一个相同字符,返回这个字符在S1中第一次出现的位置。

说明:(返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现 的子串的长度。)

  简单地说,若strcspn()返回的数值为n,则代表字符串s1开头连续有n个字符都不含字符串s2内的字符
:int strcspn(char *str1, char *str2);
程序例:

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

intmain(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;

length= strcspn(string1, string2);
printf("Character wherestrings intersect is at position %d\n", length);

return0;
}

7函数名:strdup
功能
:将串拷贝到新建的位置处
用法:char *strdup(char *str);
程序例:

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

intmain(void)
{
char *dup_str, *string = "abcde";

dup_str= strdup(string);
printf("%s\n", dup_str);
free(dup_str);//
这是C语言C++用的是delete(dup_str);

return0;
}

8函数名:stricmp
功能
:以大小写不敏感方式比较两个串
用法:int stricmp(char *str1, char *str2);
程序例:

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

intmain(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;

ptr= stricmp(buf2, buf1);

if(ptr > 0)
printf("buffer 2 is greater than buffer1\n");

if(ptr < 0)
printf("buffer 2 is less than buffer1\n");

if(ptr == 0)
printf("buffer 2 equals buffer 1\n");

return0;
}

9函数名:strerror
功能
:返回指向错误信息字符串的指针
用法:char *strerror(int errnum);
程序例:

#include<stdio.h>
#include <errno.h>

intmain(void)
{
   char *buffer;
  buffer = strerror(errno);
   printf("Error: %s\n",buffer);
   return 0;
}



10函数名:strncmp
能:比较字符串的前maxlen个字符

形式:intstrncmp(char *str1, char *str2, int maxlen)
程序例
:

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

intmain(void)

{
   char *buf1 = "aaabbb", *buf2 = "bbbccc",*buf3 = "ccc";
   int ptr;

  ptr= strncmp(buf2,buf1,3);
   if (ptr > 0)
      printf("buffer 2 is greaterthan buffer 1\n");
   else
     printf("buffer 2 is less than buffer 1\n");

  ptr= strncmp(buf2,buf3,3);
   if (ptr > 0)
      printf("buffer 2 is greaterthan buffer 3\n");
   else
     printf("buffer 2 is less than buffer 3\n");

   return(0);
}



11函数名:strncpy
:串拷贝,将第二个字符maxlen个字符拷贝给第一个字符串

:char *strncpy(char *destin, char *source, int maxlen);
程序例
:

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

intmain(void)
{
char string[10];
char *str1 = "abcdefghi";

strncpy(string,str1, 3); //只复制三个字符
string[3] = '\0';
printf("%s\n", string);

return 0;
}



12函数名:strpbrk
功能:依次检验字符串s1中的字符,当被检验字符在字符串s2中也包含时,则停止检验,并返回该字符位置,空字符NULL不包括在内。

说明:返回s1中第一个满足条件的字符的指针,如果没有匹配字符则返回空指针NULL
:char *strpbrk(char *str1, char *str2);
程序例:

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

intmain(void)
{
   char *string1 ="abcdefghijklmnopqrstuvwxyz";
   char*string2 = "onm";
   char *ptr;

   ptr= strpbrk(string1, string2);

   if(ptr)
      printf("strpbrk foundfirst character: %c\n", *ptr);
   else
     printf("strpbrk didn't find character in set\n");

   return0;
}





13函数名:strrchr
功能
:在串中查找指定字符的最后一个出现
用法:char *strrchr(char *str, char c);
程序例:

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

intmain(void)
{
   char string[15];
  char *ptr, c = 'r';

   strcpy(string,"This is a string");
  ptr = strrchr(string, c);
   if (ptr)
     printf("The character %c is at position: %d\n", c,ptr-string);
   else
     printf("The character was not found\n");
  return 0;
}



14函数名:strrev
:串倒转VC支持)

用法:char *strrev(char *str);
程序例:

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

intmain(void)
{
   char *forward = "string";

  printf("Beforestrrev(): %s\n", forward);
   strrev(forward);
  printf("After strrev(): %s\n", forward);
  return 0;
}




15函数名:strset
功能:将一个串中的所有字符都设为指定字符VC支持)

用法:char *strset(char *str, char c);
程序例:

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

intmain(void)
{
   char string[10] = "123456789";
   char symbol = 'c';

   printf("Beforestrset(): %s\n", string);
   strset(string,symbol);
   printf("After strset(): %s\n",string);
   return 0;
}




16函数名:strtod
功能
:将字符串转换为double型值
用法:double strtod(char *str, char **endptr);
程序例:

#include<stdio.h>
#include <stdlib.h>

intmain(void)
{
   char input[80], *endptr;
  double value;

   printf("Entera floating point number:");
   gets(input);
  value = strtod(input, &endptr);
   printf("Thestring is %s the number is %lf\n", input, value);
  return 0;
}

17函数名:strtok
功能
:查找由在第二个串中指定的分界符分隔开的单词
用法:char *strtok(char *str1, char *str2);
程序例:

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

intmain(void)
{
   char input[16] = "abc,d";
   char *p;

   /*strtok places a NULL terminator
   in front of thetoken, if found */
   p = strtok(input, ",");
   if (p)   printf("%s\n", p);

   /*A second call to strtok using a NULL
   as the firstparameter returns a pointer
   to the characterfollowing the token */
   p = strtok(NULL, ",");
   if (p)   printf("%s\n", p);
  return 0;
}




18函数名:strupr
功能:将串中的小写字母转换为大写字母

用法:char *strupr(char *str);
程序例:

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

intmain(void)
{
   char *string ="abcdefghijklmnopqrstuvwxyz", *ptr;

   /*converts string to upper case characters */
   ptr =strupr(string);
   printf("%s\n", ptr);
  return 0;
}

19函数名:swab
功能
:交换字节
用法:void swab (char *from, char *to, int nbytes);
程序例:

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

charsource[15] = "rFna koBlrna d";
char target[15];

intmain(void)
{
   swab(source, target,strlen(source));
   printf("This is target: %s\n",target);
   return 0;
原型:externchar *strstr(char *haystack, char *needle);
所在头文件:#include<string.h>
功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL



posted on 2012-06-15 20:28  c语言源码  阅读(387)  评论(0编辑  收藏  举报

导航