C语言字符串函数大全

2012-02-11 19:49:27|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C/C++, 字符处理|来源:唯设编程网

12.函数名: strncmp

功  能: 串比较
用  法: int strncmp(char *str1, char *str2, int maxlen);
程序例:

#include <string.h>
#include <stdio.h>
int  main(void)
{
   char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
   int ptr;
   ptr = strncmp(buf2,buf1,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1
");
   else
      printf("buffer 2 is less than buffer 1
");
   ptr = strncmp(buf2,buf3,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 3
");
   else
      printf("buffer 2 is less than buffer 3
");
   return(0);
}

13.函数名: strncmpi

功  能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用  法: int strncmpi(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{
   char *buf1 = "BBBccc", *buf2 = "bbbccc";
   int ptr;
   ptr = strncmpi(buf2,buf1,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1
");
   if (ptr < 0)
      printf("buffer 2 is less than buffer 1
");
   if (ptr == 0)
      printf("buffer 2 equals buffer 1
");
   return 0;
}

14.函数名: strncpy

功  能: 串拷贝
用  法: char *strncpy(char *destin, char *source, int maxlen);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";
   strncpy(string, str1, 3);
   string[3] = '';
   printf("%s
", string);
   return 0;
}
发表评论0条 】
网友评论(共?条评论)..
C语言字符串函数大全