String.h函数总结

合集下载

memcpy函数详解

memcpy函数详解

memcpy函数是C/C++语言中常用的内存复制函数之一,它可以将一个源地址的内存块中的数据复制到另一个目标地址的内存块中。

在本文中,我们将详细介绍memcpy函数的使用、原理、特性以及可能存在的问题。

一、函数概述memcpy函数定义在<string.h>头文件中,其函数原型如下:```c++void memcpy(void dest, const void src, size_t n);```其中,dest表示目标地址,src表示源地址,n表示要复制的字节数。

该函数返回值为指向目标地址的指针。

二、函数使用使用memcpy函数非常简单,只需要按照上述函数原型传入参数即可。

下面是一个例子:```c++include <iostream>include <cstring>using namespace std;int main(){char str1[] = "Hello World!";char str2[50];memcpy(str2, str1, strlen(str1) + 1);cout << "str2: " << str2 << endl;return 0;}```上述代码中,首先定义了一个字符串数组str1,并对其进行赋值。

然后定义了另一个空的字符串数组str2,使用memcpy函数将str1中的内容复制到str2中,并打印出str2的值。

三、函数原理memcpy函数的底层实现是通过将源地址中的数据逐个字节地复制到目标地址中来实现的。

具体而言,该函数会从源地址src开始逐个字节地复制,直到复制了n个字节或者遇到了结束符'\0'。

复制过程中,目标地址dest也会逐个字节地被复制。

因此,memcpy函数的时间复杂度为O(n),其中n表示要复制的字节数。

值得注意的是,由于该函数仅仅是逐个字节地进行复制,因此在进行内存复制时,应当确保源地址和目标地址之间没有重叠,否则可能会造成未定义的行为。

字符串删除函数

字符串删除函数

字符串删除函数1 题目介绍字符串是计算机编程中非常重要的数据类型之一,它表示一组字符序列。

在编程中,我们经常需要对字符串进行操作,在某些时候,需要删除指定的字符或字符串,本文将介绍字符串删除函数,以及如何使用它进行字符串操作。

2 字符串删除函数在 C 语言中,字符串删除函数是 strcpy(),它的作用是将一个字符串中的一部分字符删除。

其函数原型如下:``` cchar * strcpy ( char * dest, const char * src );```函数参数说明:- dest:目标字符串- src:源字符串函数返回值:返回目标字符串3 使用 strcpy() 函数删除字符串在使用 strcpy() 函数删除字符串时,需要首先明确需要删除的字符或字符串的位置,然后将其后面的所有字符依次向前移动,覆盖掉需要删除的字符或字符串。

下面列举几个使用 strcpy() 函数删除字符串的示例。

删除指定位置的单个字符如果要删除字符串中的某一个字符,可以使用 strcpy() 函数。

例如,比如我们有一个字符串 s,其中存放了一句话,“Hello World!” ,我们需要删除其中的 o 字符,可以使用下面的代码:``` cinclude <stdio.h>include <string.h>int main(){char s[] = "Hello World!";strcpy(s + 4, s + 5);printf("%s", s);return 0;}```输出结果为:Hell World!删除指定位置的多个字符如果要删除字符串中的多个字符,同样可以使用 strcpy() 函数。

例如,比如我们有一个字符串 s,其中存放了一个句子,“I love programming!” ,我们需要删除其中的 love 子串,可以使用下面的代码:``` cinclude <stdio.h>include <string.h>int main(){char s[] = "I love programming!";strcpy(s + 2, s + 7);printf("%s", s);return 0;}```输出结果为:I programming!删除指定位置的指定长度的字符如果要删除字符串中指定位置的指定长度的字符,同样可以使用strcpy() 函数。

acm常用字符串处理函数

acm常用字符串处理函数

sstrstr与strchar用法原型:extern char *strstr(char *haystack, char *needle);用法:#include <string.h>功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。

说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。

举例:#include <syslib.h>#include <string.h>main(){char *s="Golden Global View";char *l="lob";char *p;clrscr();p=strstr(s,l);if(p)printf("%s",p);elseprintf("Not Found!");getchar();return 0;}语法:int strstr(str1,str2)str1: 被查找目标string expression to search.str2:要查找对象The string expression to find.该函数返回str2第一次在str1中的位置,如果没有找到,返回NULLThe strstr() function returns the ordinal position within str1 of the first occurrence of str2. If str2 is not found in str1, strstr() returns 0.例子:功能:从字串” string1 onexxx string2 oneyyy”中寻找”yyy”(假设xxx和yyy都是一个未知的字串)char *s=” string1 onexxx string2 oneyyy”;char *p;p=strstr(s,”string2”);if(!p) printf(“Not Found!”);p=strstr(p,”one”);if(!p) printf(“Not Found!”);p+=strlen(“one”)printf(“%s”,p);说明:如果直接写语句p=strstr(p,”one”),则找到的是xxx,不符合要求所以需采用二次查找法找到目标用法:#include <string.h>原型: extern char *strchr(char* str1, char c)返回c在str1中首次出现的位置的指针,若没有找到,返回NULL#include <stdio.h>#include <string.h>main(){char*s="Golden Global View" ;char*p ;p=strchr(s,'l');if(p)printf("%s",p);elseprintf("Not Found!");return 0 ;}ACM 字符串KMP算法模板与用途直s接用KMP算法真的去匹配两个字符串其实很少见,除非字符串里的字符集范围很小,或字符重复数量过多,用KMP可大减少时间,否则一般都是直接朴素匹配。

头文件cstringstringstring.h、以及stringCString的区别

头文件cstringstringstring.h、以及stringCString的区别

头⽂件cstringstringstring.h、以及stringCString的区别
1.头⽂件cstring/string/string.h的区别
string.h,⾯向的是C风格的字符串,处理的是char*,'\0'结尾的这种类型的字符串,包括⽐如strcpy之类的字符串处理函数,
没有加⼊到C++标准库中。

调⽤的时候使⽤全局命名空间。

cstring,则是兼容C的头⽂件,⾥⾯的函数是基于C或者说C风格的⽐如strcpy之类的字符串处理函数,加⼊到C++库中,
被定义的std命名空间,调⽤的时候需要使⽤std命名空间。

string,是C++定义的定义的std::string所使⽤的⽂件,是string类的头⽂件,属于STL范畴。

调⽤的时候需要使⽤std命名空间。

2.CS tring/string的区别
CString类是微软的visual c++提供的MFC⾥⾯的⼀个类,所以只有⽀持MFC的⼯程才可以使⽤,并不在std命名空间中,
因为它不是c++的标准库,只是微软的⼀个封装库。

string类是在c++标准库中,所以它被封装在了std命名空间中,使⽤之前需要声明using namespace std,string类的程序的移植性更好。

linux上的⼯程就不能⽤CString了,只能⽤标准C++中的string类了。

c语言中判断一个字符串是否包含另一个字符串

c语言中判断一个字符串是否包含另一个字符串

c语⾔中判断⼀个字符串是否包含另⼀个字符串转载⾃:1. 使⽤库函数string.hstrstr函数函数名: strstr功 能: 在串中查找指定字符串的第⼀次出现⽤ 法: char *strstr(char *str1, char *str2);说明:返回指向第⼀次出现str2位置的指针,如果没找到则返回NULL。

调⽤函数,判断返回值是否等于NULL,决定是否输出如;char *p=".c"; //需要的⼦串if(strstr(A,p)) printf("%s",A);2. 不使⽤库函数第⼀⾏是要查找的字符串第⼆⾏是被查找句⼦输出要查找的字符串在被查找句⼦的⾸个位置#include<stdio.h>#include<string.h>void main(){int i,j,la,lb,num=0;char c,a[120],b[4500];while((c=getchar())!='\n') a[num++]=c;a[num]='\0';num=0;while((c=getchar())!='\n') b[num++]=c;b[num]='\0';la=strlen(a);lb=strlen(b);for(i=0;(lb-i)>=la;i++){if(b[i]==a[0]){for(j=0;j<la;j++)if(b[i+j]!=a[j]) break;if(j==la) {printf("%d\n",i+1);break;}}}}3. 使⽤指针#include "stdio.h"#include "conio.h"#include "string.h"int main(){char str[20]="jsdlaadf",substr[10]="sdf"; char *p=str,*q=substr;int flag=0;for(;*(p+strlen(substr)-1);p++){for(q=substr;*p==*q&&*q;p++,q++);if(!*q){flag=1;break;}}if(flag==1) puts("accord");else puts("not accord");system("pause");}。

C语言~所有的字符串操作函数

C语言~所有的字符串操作函数
strcpy(string, str1); printf("%s\n", string); return 0; }
7.函数名: strcspn 功 能: 在串中查找第一个给定字符集内容的段 用 法: int strcspn(char *str1, char *str2); 程序例: #i nclude <stdio.h> #i nclude <string.h> #i nclude <alloc.h> int main(void) { char *string1 = "1234567890"; char *string2 = "747DC8"; int length;
printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)
printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)
printf("buffer 2 equals buffer 1\n"); return 0; }
printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)
printf("buffer 2 equals buffer 1\n"); return 0; }
14.函数名: strncpy 功 能: 串拷贝 用 法: char *strncpy(char *destin, char *source, int maxlen); 程序例: #i nclude <stdio.h> #i nclude <string.h> int main(void) {

C语言字符串函数大全

C语言字符串函数大全

函数名: stpcpy功能: 拷贝一个字符串到另一个用法: char *stpcpy(char *destin, char *source); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";stpcpy(string, str1);printf("%s\n", string);return 0;}函数名: strcat功能: 字符串拼接函数用法: char *strcat(char *destin, char *source);程序例:#include <string.h>#include <stdio.h>int main(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;}函数名: strchr功能: 在一个串中查找给定字符的第一个匹配之处\用法: char *strchr(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 = strchr(string, c);if (ptr)printf("The character %c is at position: %d\n", c, ptr-string);elseprintf("The character was not found\n");return 0;}函数名: 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\n");elseprintf("buffer 2 is less than buffer 1\n");ptr = strcmp(buf2, buf3);if (ptr > 0)printf("buffer 2 is greater than buffer 3\n");elseprintf("buffer 2 is less than buffer 3\n");return 0;}函数名: strncmpi功能: 将一个串中的一部分与另一个串比较, 不管大小写用法: int strncmpi(char *str1, char *str2, unsigned maxle n);程序例:#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\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: 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\n", string);return 0;}函数名: 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\n", length);return 0;}函数名: strdup功能: 将串拷贝到新建的位置处用法: char *strdup(char *str);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *dup_str, *string = "abcde";dup_str = strdup(string);printf("%s\n", dup_str);free(dup_str);return 0;}函数名: stricmp功能: 以大小写不敏感方式比较两个串用法: int stricmp(char *str1, char *str2); 程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb"; int ptr;ptr = stricmp(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strerror功能: 返回指向错误信息字符串的指针用法: char *strerror(int errnum);程序例:#include <stdio.h>#include <errno.h>int main(void){char *buffer;buffer = strerror(errno);printf("Error: %s\n", buffer);return 0;}函数名: strcmpi功能: 将一个串与另一个比较, 不管大小写用法: int strcmpi(char *str1, char *str2);程序例:#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\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: 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\n");elseprintf("buffer 2 is less than buffer 1\n");ptr = strncmp(buf2,buf3,3);if (ptr > 0)printf("buffer 2 is greater than buffer 3\n");elseprintf("buffer 2 is less than buffer 3\n");return(0);}函数名: 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\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strncpy功能: 串拷贝用法: char *strncpy(char *destin, char *source, int maxle n);程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";strncpy(string, str1, 3);string[3] = '\0';printf("%s\n", string);return 0;}函数名: strnicmp功能: 不注重大小写地比较两个串用法: int strnicmp(char *str1, char *str2, unsigned maxle n);程序例:#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\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: 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\n", string); strnset(string, letter, 13);printf("string after strnset: %s\n", string);return 0;}函数名: 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\n", *ptr); elseprintf("strpbrk didn't find character in set\n");return 0;}函数名: 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\n", c, ptr-string);elseprintf("The character was not found\n");return 0;函数名: strrev功能: 串倒转用法: char *strrev(char *str);程序例:#include <string.h>#include <stdio.h>int main(void){char *forward = "string";printf("Before strrev(): %s\n", forward); strrev(forward);printf("After strrev(): %s\n", forward); return 0;}函数名: strset功能: 将一个串中的所有字符都设为指定字符用法: char *strset(char *str, char c);程序例:#include <stdio.h>#include <string.h>int main(void){char string[10] = "123456789";char symbol = 'c';printf("Before strset(): %s\n", string);strset(string, symbol);printf("After strset(): %s\n", string);return 0;}函数名: strspn功能: 在串中查找指定字符集的子集的第一次出现程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *string1 = "1234567890";char *string2 = "123DC8";int length;length = strspn(string1, string2);printf("Character where strings differ is at position %d\n ", length);return 0;}函数名: strstr功能: 在串中查找指定字符串的第一次出现程序例:#include <stdio.h>#include <string.h>int main(void){char *str1 = "Borland International", *str2 = "nation", * ptr;ptr = strstr(str1, str2);printf("The substring is: %s\n", ptr);return 0;}函数名: strtod功能: 将字符串转换为double型值用法: double strtod(char *str, char **endptr);程序例:#include <stdio.h>#include <stdlib.h>{char input[80], *endptr;double value;printf("Enter a floating point number:");gets(input);value = strtod(input, &endptr);printf("The string is %s the number is %lf\n", input, val ue);return 0;}函数名: strtok功能: 查找由在第二个串中指定的分界符分隔开的单词用法: char *strtok(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>{char input[16] = "abc,d";char *p;/* strtok places a NULL terminatorin front of the token, if found */p = strtok(input, ",");if (p) printf("%s\n", p);/* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, ",");if (p) printf("%s\n", p);return 0;}函数名: strtol功能: 将串转换为长整数用法: long strtol(char *str, char **endptr, int base); 程序例:#include <stdlib.h>#include <stdio.h>int main(void){char *string = "87654321", *endptr;long lnumber;/* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10);printf("string = %s long = %ld\n", string, lnumber);return 0;}函数名: strupr功能: 将串中的小写字母转换为大写字母用法: char *strupr(char *str);程序例:#include <stdio.h>#include <string.h>int main(void){char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;/* converts string to upper case characters */ptr = strupr(string);printf("%s\n", ptr);return 0;}函数名: swab功能: 交换字节用法: void swab (char *from, char *to, int nbytes); 程序例:#include <stdlib.h>#include <stdio.h>#include <string.h>char source[15] = "rFna koBlrna d"; char target[15];int main(void){swab(source, target, strlen(source)); printf("This is target: %s\n", target); return 0;}。

C语言字符串处理掌握字符串的输入输出和处理函数

C语言字符串处理掌握字符串的输入输出和处理函数

C语言字符串处理掌握字符串的输入输出和处理函数C语言字符串处理:掌握字符串的输入输出和处理函数在C语言中,处理字符串是十分重要的。

字符串是一系列字符的集合,在程序中广泛应用于文本处理和数据操作。

掌握字符串的输入输出和处理函数对于编写高效的C语言程序至关重要。

本文将介绍C语言中字符串的输入输出和一些常用的字符串处理函数。

一、字符串的输入输出1. 字符串的输入在C语言中,我们可以使用scanf函数来读取字符串的输入。

需要注意的是,由于scanf遇到空格、制表符或换行符时会停止读取,因此无法直接读取带有空格的字符串。

为了读取完整的字符串,我们可以使用fgets函数。

```c#include <stdio.h>int main() {char str[100];printf("请输入一个字符串:");fgets(str, sizeof(str), stdin);printf("您输入的字符串是:%s\n", str);return 0;}```上述代码中,我们定义了一个大小为100的字符数组str,并使用fgets函数从标准输入读取字符串。

其中sizeof(str)用于指定最大读取的字符数。

2. 字符串的输出在C语言中,我们可以使用printf函数来输出字符串。

需要注意的是,如果字符串中包含格式控制符(如%),需要使用转义字符%来表示。

```c#include <stdio.h>int main() {char str[] = "Hello, World!";printf("字符串输出示例:\n");printf("%s\n", str);return 0;}```上述代码中,我们定义了一个包含字符串"Hello, World!"的字符数组str,并使用printf函数输出该字符串。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
5、strncmpi
函数名: 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\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); return 0; }
6、strcpy
函数名: 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\n", string); return 0;
2、strcat 函数名: strcat 功 能: 字符串拼接函数 用 法: char *strcat(char *destin, char *source); 程序例: #include <string.h> #include <stdio.h> int main(void)
{ char destination[25]; char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland); strcat(destination, blank); strcat(destination, c); printf("%s\n", destination); retu strchr 功 能: 在一个串中查找给定字符的第一个匹配之处\ 用 法: char *strchr(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 = 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; }
}
7、strcspn
函数名: 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\n", length); return 0; }
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 is less than buffer 3\n"); return 0; }
1、c++中 string 与 string.h 的作用和区别 答:一般一个 C++的老的带“.h”扩展名的库文件,比如 iostream.h,
在新标准后的标准库中都有一个不带“.h”扩展名的相对应,区别除了后者 的好多改进之外,还有一点就是后者的东东都塞进了“std”名字空间中。
但唯独 string 特别。 问题在于 C++要兼容 C 的标准库,而 C 的标准库里碰巧也已经有一个名字 叫做“string.h”的头文件,包含一些常用的 C 字符串处理函数。 这个头文件跟 C++的 string 类半点关系也没有,所以 <string>并非 <string.h>的“升级版本”,他们是毫无关系的两个头文件。
2、c++ <string.h>中包括哪些函数? 答:常用函数如下: strlen 求字符串长度 strcmp 比较 2 个字符串是否一样 strcat 字符串连接操作 strcpy 字符串拷贝操作 strstr 查询子串
下面为 string.h 文件中函数的详细用法,附加实例:
1、strcpy 函数名: strcpy 功 能: 拷贝一个字符串到另一个 用 法: char *strcpy(char *destin, char *source); 程序例: #include <stdio.h> #include <string.h> int main(void) { char string[10]; char *str1 = "abcdefghi"; strcpy(string, str1); printf("%s\n", string); return 0; }
4、strcmp
函数名: 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\n");
10、strerror
函数名: strerror 功 能: 返回指向错误信息字符串的指针 用 法: char *strerror(int errnum); 程序例: #include <stdio.h> #include <errno.h> int main(void) { char *buffer; buffer = strerror(errno); printf("Error: %s\n", buffer); return 0; }
8、strdup
函数名: strdup 功 能: 将串拷贝到新建的位置处 用 法: char *strdup(char *str); 程序例: #include <stdio.h> #include <string.h> #include <alloc.h> int main(void) { char *dup_str, *string = "abcde"; dup_str = strdup(string); printf("%s\n", dup_str); free(dup_str); return 0; }
13、strnicmp
函数名: 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\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); return 0; }
相关文档
最新文档