C语言字符串函数大全

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

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

15.函数名: strnicmp

功  能: 不注重大小写地比较两个串
用  法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{
   char *buf1 = "BBBccc", *buf2 = "bbbccc";
   int ptr;
   ptr = strnicmp(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;
}

16.函数名: strnset

功  能: 将一个串中的所有字符都设为指定字符
用  法: char *strnset(char *str, char ch, unsigned n);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';
   printf("string before strnset: %s
", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s
", string);
   return 0;
}

17.函数名: strpbrk

功  能: 在串中查找给定字符集中的字符
用  法: char *strpbrk(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{
   char *string1 = "abcdefghijklmnopqrstuvwxyz";
   char *string2 = "onm";
   char *ptr;
   ptr = strpbrk(string1, string2);
   if (ptr)
      printf("strpbrk found first character: %c
", *ptr);
   else
      printf("strpbrk didn't find character in set
");
   return 0;
}

18.函数名: strrchr

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

#include <string.h>
#include <stdio.h>
int main(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
", c, ptr-string);
   else
      printf("The character was not found
");
   return 0;
}
发表评论0条 】
网友评论(共?条评论)..
C语言字符串函数大全