C语言字符串函数大全

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

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

4.函数名: strcmp

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

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

5.函数名: strncmpi

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

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

6.函数名: strcpy

功  能: 串拷贝
用  法: char *strcpy(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
 {
    char string[10];
    char *str1 = "abcdefghi";
    strcpy(string, str1);
    printf("%s
", string);
    return 0;
 }

7.函数名: strcspn

功  能: 在串中查找第一个给定字符集内容的段
用  法: int strcspn(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
 {
    char *string1 = "1234567890";
    char *string2 = "747DC8";
    int length;
    length = strcspn(string1, string2);
    printf("Character where strings intersect is at position %d
", length);
    return 0;
 }
发表评论0条 】
网友评论(共?条评论)..
C语言字符串函数大全