当前位置:首页 » 编程语言 » stingc语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

stingc语言

发布时间: 2023-03-24 10:19:34

A. c语言中string指令是什么

string 是 c++的,不是 c 的
stirng 是C++里面一个用来处理字符串的类。包含了字符串处理掉一下常用方法,如:

Constructors 构造函数,用于字符串初始化
Operators 操作符,用于字符串比较和赋值
append() 在字符串的末尾添加文本
assign() 为字符串赋新值
at() 按给定索引值返回字符
begin() 返回一个迭代器,指向第一个字符
c_str() 将字符串以C字符数组的形式返回
capacity() 返回重新分配空间前的字符容量
compare() 比较两个字符串
() 将内容复制为一个字符数组
data() 返回内容的字符数组形式
empty() 如果字符串为空,返回真
end() 返回一个迭代器,指向字符串的末尾。(最后一个字符的下一个位置)
erase() 删除字符
find() 在字符串中查找字符
find_first_of() 查找第一个与value中的某值相等的字符
find_first_not_of() 查找第一个与value中的所有值都不相等的字符
find_last_of() 查找最后一个与value中的某值相等的字符
find_last_not_of() 查找最后一个与value中的所有值都不相等的字符
get_allocator() 返回配置器
insert() 插入字符
length() 返回字符串的长度
max_size() 返回字符的最大可能个数
rbegin() 返回一个逆向迭代器,指向最后一个字符
rend() 返回一个逆向迭代器,指向第一个元素的前一个位置
replace() 替换字符
reserve() 保留一定容量以容纳字符串(设置capacity值)
resize() 重新设置字符串的大小
rfind() 查找最后一个与value相等的字符(逆向查找)
size() 返回字符串中字符的数量
substr() 返回某个子字符串
swap() 交换两个字符串的内容

B. string在c语言中是什么意思

String是C++、java、VB等编程语言中的字符串,用双引号引起来的几个字符,如"Abc","一天"。

在java、C#中,String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。 String 对象是System.Char对象的有序集合,用于表示字符串。String 对象的值是该有序集合的内容,并且该值是不可变的。

(2)stingc语言扩展阅读:

string字符创的用法:

1、字符串输出函数puts,格式:puts (字符数组名),功能:把字符数组中的字符串输出到显示器。

2、字符串输入函数gets,格式:gets (字符数组名),功能:从标准输入设备键盘上输入一个字符串。 本函数得到一个函数值,即为该字符数组的首地址。

3、字符串连接函数strcat,格式:strcat (字符数组名1,字符数组名2),功能:把字符数组2中的字符串连接到字符数组1中字符串的后面,并删去字符串1后的串标志“”。本函数返回值是字符数组1的首地址。

参考资料来源:网络-string

C. C语言中有string吗

C语言中没有string类型。

string类型是 C++、java、VB等编程语言中的。 在java、C#中,String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。string 是C++标准程序库中的一个头文件,定义了C++标准中的字符串的基本模板类std::basic_string及相关的模板类实例。

而在C语言中,C语言中没有字符串类型,字符串是存放在字符型数组中的,将字符串作为字符数组来处理的。为了测定字符串的实际长度,C语言规定了一个“字符串结束标志”,以字符''作为结束标志 。

(3)stingc语言扩展阅读:

C++中string的使用:

其中的string是以char作为模板参数的模板类实例,把字符串的内存管理责任由string负责而不是由编程者负责,大大减轻了C语言风格的字符串的麻烦。std::basic_string提供了大量的字符串操作函数,如比较、连接、搜索、替换、获得子串等。并可与C语言风格字符串双向转换。

字符串在C语言中的使用:

字符串常量初始化数组。如:char c[ ]={‘c’,‘ ’,‘p’,‘r’,‘o','g','r','a','m'};可写为:char c[ ]={"C program"}; 或去掉{}写为:char c[ ]="C program"。

D. C语言有String吗

C语言租备中没有string,除用字符数组外槐缓,还可以弊明毁用指针表示,比如
char *string="C yuyan"

E. c语言中string是啥意思啊控制啥的啊

编程语言中的字符串,用双引号引起来的几个字符.如"Abc","一天"。String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象。

这样的话把String类的引用传递给一个方法,该方法对String的任何改变,对原引用指向的对象没有任何影响,这一点和基本数据类型相似。

strings1,s2;

s1="abc";

s2=s1;

s2="def";

1、用法

string类的构造函数:

string(constchar*s);//用c字符串s初始化string(intn,charc);//用n个字符c初始化

此外,string类还支持默认构造函数和复制构造函数,如strings1;strings2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常。

2、string类的字符操作:

constchar&operator[](intn)const;constchar&at(intn)const;char&operator[](intn);char&at(intn);

operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问

constchar*data()const;//返回一个非null终止的c字符数组constchar*c_str()const;//返回一个以null终止的c字符串

int(char*s,intn,intpos=0)const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。

(5)stingc语言扩展阅读:

1、string的特性描述:

intcapacity()const;//返回当前容量(即string中不必增加内存即可存放的元素个数)

intmax_size()const;//返回string对象中可存放的最大字符串的长度

intsize()const;//返回当前字符串的大岩顷小

intlength()const;//返回当前字符串的长度

boolempty()const;//当前字符串是否为空

voidresize(intlen,charc);//把字符串当前大小置为len,并用字符c填充不足的部分

2、string类的输入输出操作:

string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。函数getline(istream&in,string&s);用于从输入流in中读取字符串到s中,以换行符' '分开。

3、string的赋值:

string&operator=(conststring&s);//把字符串s赋给当前字符串

string&assign(constchar*s);//用c类型字符串s赋值

string&assign(constchar*s,intn);//用c字符串s开始的n个字符赋值

string&assign(conststring&s);//把字符串s赋给当前字符串

string&assign(intn,charc);//用n个字符c赋值世配给当前字符串

string&assign(conststring&s,intstart,intn);//把字符粗返陆串s中从start开始的n个字符赋给当前字符

string&assign(const_iteratorfirst,const_itertorlast);//把first和last迭代器之间的部分赋给字符串

4、string的连接:

string&operator+=(conststring&s);//把字符串s连接到当前字符串的结尾

string&append(constchar*s);//把c类型字符串s连接到当前字符串结尾

string&append(constchar*s,intn);//把c类型字符串s的前n个字符连接到当前字符串结尾

string&append(conststring&s);//同operator+=()

string&append(conststring&s,intpos,intn);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾

string&append(intn,charc);//在当前字符串结尾添加n个字符c

string&append(const_iteratorfirst,const_iteratorlast);//把迭代器first和last之间的部分连接到当前字符串的结尾

5、string的子串:

stringsubstr(intpos=0,intn=npos)const;//返回pos开始的n个字符组成的字符串

6、string的交换:

voidswap(string&s2);//交换当前字符串与s2的值

7、string类的查找函数:

intfind(charc,intpos=0)const;//从pos开始查找字符c在当前字符串的位置

intfind(constchar*s,intpos=0)const;//从pos开始查找字符串s在当前串中的位置

intfind(constchar*s,intpos,intn)const;//从pos开始查找字符串s中前n个字符在当前串中的位置

intfind(conststring&s,intpos=0)const;//从pos开始查找字符串s在当前串中的位置//查找成功时返回所在位置,失败返回string::npos的值

intrfind(charc,intpos=npos)const;//从pos开始从后向前查找字符c在当前串中的位置

intrfind(constchar*s,intpos=npos)const;

intrfind(constchar*s,intpos,intn=npos)const;

intrfind(conststring&s,intpos=npos)const;//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值

intfind_first_of(charc,intpos=0)const;//从pos开始查找字符c第一次出现的位置

intfind_first_of(constchar*s,intpos=0)const;

intfind_first_of(constchar*s,intpos,intn)const;

intfind_first_of(conststring&s,intpos=0)const;//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos

intfind_first_not_of(charc,intpos=0)const;

intfind_first_not_of(constchar*s,intpos=0)const;

intfind_first_not_of(constchar*s,intpos,intn)const;

intfind_first_not_of(conststring&s,intpos=0)const;//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos

intfind_last_of(charc,intpos=npos)const;

intfind_last_of(constchar*s,intpos=npos)const;

intfind_last_of(constchar*s,intpos,intn=npos)const;

intfind_last_of(conststring&s,intpos=npos)const;

intfind_last_not_of(charc,intpos=npos)const;

intfind_last_not_of(constchar*s,intpos=npos)const;

intfind_last_not_of(constchar*s,intpos,intn)const;

intfind_last_not_of(conststring&s,intpos=npos)const;//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找。

string类的替换函数:

string&replace(intp0,intn0,constchar*s);//删除从p0开始的n0个字符,然后在p0处插入串s

string&replace(intp0,intn0,constchar*s,intn);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符

string&replace(intp0,intn0,conststring&s);//删除从p0开始的n0个字符,然后在p0处插入串s

string&replace(intp0,intn0,conststring&s,intpos,intn);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符

string&replace(intp0,intn0,intn,charc);//删除p0开始的n0个字符,然后在p0处插入n个字符c

string&replace(iteratorfirst0,iteratorlast0,constchar*s);//把[first0,last0)之间的部分替换为字符串s

string&replace(iteratorfirst0,iteratorlast0,constchar*s,intn);//把[first0,last0)之间的部分替换为s的前n个字符。

string&replace(iteratorfirst0,iteratorlast0,conststring&s);//把[first0,last0)之间的部分替换为串s

string&replace(iteratorfirst0,iteratorlast0,intn,charc);//把[first0,last0)之间的部分替换为n个字符c

string&replace(iteratorfirst0,iteratorlast0,const_iteratorfirst,const_iteratorlast);//把[first0,last0)之间的部分替换成[first,last)之间的字符串。

string类的插入函数:

string&insert(intp0,constchar*s);

string&insert(intp0,constchar*s,intn);

string&insert(intp0,conststring&s);

string&insert(intp0,conststring&s,intpos,intn);//前4个函数在p0位置插入字符串s中pos开始的前n个字符

string&insert(intp0,intn,charc);//此函数在p0处插入n个字符c

iteratorinsert(iteratorit,charc);//在it处插入字符c,返回插入后迭代器的位置

voidinsert(iteratorit,const_iteratorfirst,const_iteratorlast);//在it处插入[first,last)之间的字符

voidinsert(iteratorit,intn,charc);//在it处插入n个字符c

string类的删除函数

iteratorerase(iteratorfirst,iteratorlast);//删除[first,last)之间的所有字符,返回删除后迭代器的位置。

iteratorerase(iteratorit);//删除it指向的字符,返回删除后迭代器的位置。

string&erase(intpos=0,intn=npos);//删除pos开始的n个字符,返回修改后的字符串。

string类的迭代器处理:

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。

用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

const_iteratorbegin()const;iteratorbegin();//返回string的起始位置

const_iteratorend()const;iteratorend();//返回string的最后一个字符后面的位置

const_iteratorrbegin()const;iteratorrbegin();//返回string的最后一个字符的位置

const_iteratorrend()const;iteratorrend();//返回string第一个字符位置的前面

rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator或string::const_reverse_iterator实现

字符串流处理:

通过定义ostringstream和istringstream变量实现,在#include<sstream>头文件中。

例如:

stringinput("hello,thisisatest");

istringstreamis(input);

strings1,s2,s3,s4;

is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"

ostringstreamos;

os<<s1<<s2<<s3<<s4;

cout<<os.str();

F. C语言string是什么数据类型

C语言中只有String文件,并没有String类型

G. string在c语言中是什么意思

string在c语言中的意思是System.Char对象的有序集合,用于表示字符串。

String就是C等编程语言中的字符串,用双引号引起来的几个字符。

在java、C#中,String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。String对象是System.Char对象的有序集合,用于表示字符串。String对象的值是该有序集合的内容,并且该值是不可变的。

可以使用以下方法之一来创建string对象:

通过给String变量指定一个字符串。

通过使用String类构造函数。

通过使用字符串串联运算符(+)。

通过检索属性或调用一个返回字符串的方法。

通过格式化方法来转换一个值或对象为它的字符串表示形式。

H. c语言string的用法大全

C语言是一门面向过程的、抽象化的通用程或孝陪序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处慎坦理低级存储器。C 语言string的用法有哪些呢,请看看下面我为你整理 总结 的c语言string的用法大全_C语言中string使用 方法 。

c语言string的用法

函数原型:char *strp(const char *s)

函数功能:字符串拷贝,目的空间由该函数分配

函数返回:指向拷贝后的字符串指针

参数说明:src-待拷贝的源字符串

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

#include<alloc.h>

intmain()

{

char*p_str,*string="abcde";

p_str=strp(string);

printf("%s",p_str);

free(p_str);

return0;

}

@函数名称:strcpy

函数原型:char* strcpy(char* str1,char* str2);

函数功能:把str2指向的字符串拷贝到str1中去

函数返回:返回str1,即指向str1的指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[10];

char*str1="abcdefghi";

strcpy(string,str1);

printf("thestringis:%s ",string);

return0;

}

@函数名称:strncpy

函数原型:char *strncpy(char *dest, const char *src,intcount)

函数功能:将字符串src中的count个字符拷贝到字符串dest中去

函数返回:指向dest的指针

参数说明:dest-目的字符串,src-源字符串,count-拷贝的字符个数

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*src="bbbbbbbbbbbbbbbbbbbb";//20'b's

chardest[50]="aaaaaaaaaaaaaaaaaaaa";//20'a's

puts(dest);

strncpy(dest,src,10);

puts(dest);

return0;

}

输出:

[cpp] view plain

/*******************************************

aaaaaaaaaaaaaaaaaaaa

bbbbbbbbbbaaaaaaaaaa

*******************************************/

注意:strncpy只复制指定长度的字符,不会自动在末尾加''。若指定长度超过源字符串长度,不够的部分补‘’,

@函数名称:strcat

函数原型:char* strcat(char * str1,char * str2);

函数功能:把字符串str2接到衫蠢str1后面,str1最后的''被取消

函数返回:str1

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charbuffer[80];

strcpy(buffer,"Hello");

strcat(buffer,"world");

printf("%s ",buffer);

return0;

}

@函数名称:strncat

函数原型:char *strncat(char *dest, const char *src, size_t maxlen)

函数功能:将字符串src中前maxlen个字符连接到dest中

函数返回:

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

charbuffer[80];

intmain()

{

strcpy(buffer,"Hello");

strncat(buffer,"world",8);

printf("%s ",buffer);

strncat(buffer,"*************",4);

printf("%s ",buffer);

return0;

}

注意:与strncpy不同的是,strncat会自动在末尾加‘’,若指定长度超过源字符串长度,则只复制源字符串长度即停止

@函数名称:strcmp

函数原型:int strcmp(char * str1,char * str2);

函数功能:比较两个字符串str1,str2.

函数返回:str1<str2,返回负数;str1=str2,返回 0;str1>str2,返回正数.

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*buf1="aaa",*buf2="bbb",*buf3="ccc";

intptr;

ptr=strcmp(buf2,buf1);

if(ptr>0)

printf("buffer2isgreaterthanbuffer1 ");

else

printf("buffer2islessthanbuffer1 ");

ptr=strcmp(buf2,buf3);

if(ptr>0)

printf("buffer2isgreaterthanbuffer3 ");

else

printf("buffer2islessthanbuffer3 ");

return0;

}

@函数名称:strncmp

函数原型:int strncmp(char *str1,char *str2,int count)

函数功能:对str1和str2中的前count个字符按字典顺序比较

函数返回:小于0:str1<str2,等于0:str1=str2,大于0:str1>str2

参数说明:str1,str2-待比较的字符串,count-比较的长度

所属文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstr1[]="aabbc";//

charstr2[]="abbcd";//

//为使测试程序更简练,此处假定了strncmp只返回-1,0,1三个数

charres_info[]={'<','=','>'};

intres;

//前1个字符比较

res=strncmp(str1,str2,1);

printf("1:str1%cstr2 ",res_info[res+1]);

//前3个字符比较

res=strncmp(str1,str2,3);

printf("3:str1%cstr2 ",res_info[res+1]);

}

输出:

[cpp] view plain

/****************************************

1:str1=str2

3:str1<str2

*****************************************/

@函数名称:strpbrk

函数原型:char *strpbrk(const char *s1, const char *s2)

函数功能:得到s1中第一个“同时也出现在s2中”字符的位置指针

函数返回:位置指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*p="Findallvowels";

p=strpbrk(p+1,"aeiouAEIOU");

while(p)

{

printf("%s ",p);

p=strpbrk(p+1,"aeiouAEIOU");

}

return0;

}

输出:

[cpp] view plain

/**************************************

indallvowels

allvowels

owels

els

**************************************/

@函数名称:strcspn

函数原型:int strcspn(const char *s1, const char *s2)

函数功能:统计s1中从头开始直到第一个“来自s2中的字符”出现的长度

函数返回:长度

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

printf("%d ",strcspn("abcbcadef","cba"));

printf("%d ",strcspn("xxxbcadef","cba"));

printf("%d ",strcspn("123456789","cba"));

return0;

}

输出:

[cpp] view plain

/************************

0

3

9

************************/

@函数名称:strspn

函数原型:int strspn(const char *s1, const char *s2)

函数功能:统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度

函数返回:位置指针

参数说明:

所属文件:<string.h>

[html] view plain

#include<stdio.h>

#include<string.h>

#include<alloc.h>

intmain()

{

printf("%d ",strspn("abcbcadef","cba"));

printf("%d ",strspn("xxxbcadef","cba"));

printf("%d ",strspn("123456789","cba"));

return0;

}

输出:

[cpp] view plain

/************************

6

0

0

************************/

@函数名称:strchr

函数原型:char* strchr(char* str,char ch);

函数功能:找出str指向的字符串中第一次出现字符ch的位置

函数返回:返回指向该位置的指针,如找不到,则返回空指针

参数说明:str-待搜索的字符串,ch-查找的字符

所属文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*str="Thisisastring!";

charch;

char*p;

while(1)

{

printf("Pleaseinputachar:");

ch=getchar();

p=strchr(str,ch);

if(p)

printf("%cisthe%dcharacterof"%s" ",ch,(int)(p-str+1),str);

else

printf("Notfound! ");

printf("PressESCtoquit! ");

if(27==getch())

break;

fflush(stdin);

}

return0;

}

运行结果:

[cpp] view plain

/********************************************

Pleaseinputachar:i

iisthe3characterof"Thisisastring!"

PressESCtoquit!

Pleaseinputachar:l

Notfound!

PressESCtoquit!

Pleaseinputachar:s

sisthe4characterof"Thisisastring!"

PressESCtoquit!

**********************************************/

@函数名称:strrchr

函数原型:char *strrchr(const char *s, int c)

函数功能:得到字符串s中最后一个含有c字符的位置指针

函数返回:位置指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstring[15];

char*ptr,c='r';

strcpy(string,"Thisisastring");

ptr=strrchr(string,c);

if(ptr)

printf("Thecharacter%cisatposition:%d",c,ptr-string);

else

printf("Thecharacterwasnotfound");

return0;

}

@函数名称:strstr

函数原型:char* strstr(char* str1,char* str2);

函数功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)

函数返回:返回该位置的指针,如找不到,返回空指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;

ptr=strstr(str1,str2);

printf("Thesubstringis:%s ",ptr);

return0;

}

输出:

The substringis:Watcom C/C++

@函数名称:strrev

函数原型:char *strrev(char *s)

函数功能:将字符串中的所有字符颠倒次序排列

函数返回:指向s的指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charforward[]="string";//原文中定义为char*是不对的,指向代码段的指针内容是不可变的

printf("Beforestrrev():%s",forward);

strrev(forward);

printf("Afterstrrev():%s",forward);

return0;

}

输出:

[cpp] view plain

/************************************

Beforestrrev():string

Afterstrrev():gnirts

************************************/

@函数名称:strnset

函数原型:char *strnset(char *s, int ch, size_t n)

函数功能:将字符串s中前n个字符设置为ch的值

函数返回:指向s的指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";

charletter='x';

printf("stringbeforestrnset:%s ",string);

strnset(string,letter,10);

printf("stringafterstrnset:%s ",string);

return0;

}

输出:

[cpp] view plain

/*************************************************

stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa

stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa

*************************************************/

@函数名称:strset

函数原型:char *strset(char *s, int ch)

函数功能:将字符串s中所有字符设置为ch的值

函数返回:指向s的指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[10]="123456789";

charsymbol='c';

printf("Beforestrset():%s",string);

strset(string,symbol);

printf("Afterstrset():%s",string);

return0;

}

@函数名称:strtok

函数原型:char *strtok(char *s1, const char *s2)

函数功能:分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词)

函数返回:字符串s1中首次出现s2中的字符前的子字符串指针

参数说明:s2一般设置为s1中的分隔字符

规定进行子调用时(即分割s1的第二、三及后续子串)第一参数必须是NULL

在每一次匹配成功后,将s1中分割出的子串位置替换为NULL(摘下链中第一个环),因此s1被破坏了

函数会记忆指针位置以供下一次调用

所属文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*p;

char*buffer;

char*delims={".,"};

buffer=strp("Findwords,allofthem.");

printf("%s ",buffer);

p=strtok(buffer,delims);

while(p!=NULL){

printf("word:%s ",p);

p=strtok(NULL,delims);

}

printf("%s ",buffer);

return0;

}//根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔

PS:根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔

@函数名称:strupr

函数原型:char *strupr(char *s)

函数功能:将字符串s中的字符变为大写

函数返回:

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//会影响原字符串的内存,用char[]来声明

ptr=strupr(string);

printf("%s",ptr);

return0;

}

@函数名称:strlwr

函数原型:char *strlwr(char *s)

函数功能:将字符串中的字符变为小写字符

函数返回:指向s的指针

参数说明:

所属文件:<string.h>

[cpp] view plain

#include<string.h>

intmain()

{

charstr[]="HOWTOSAY";

printf("%s",strlwr(str));

return0;

}

@函数名称:strerror

函数原型:char *strerror(int errnum)

函数功能:得到错误信息的内容信息

函数返回:错误提示信息字符串指针

参数说明:errnum-错误编号

所属文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<errno.h>

intmain()

{

char*buffer;

buffer=strerror(errno);

printf("Error:%s",buffer);

return0;

}

@函数名称:memcpy

函数原型:void *memcpy(void *dest, const void *src, size_t n)

函数功能:字符串拷贝

函数返回:指向dest的指针

参数说明:src-源字符串,n-拷贝的最大长度

所属文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charsrc[]="******************************";

chardest[]="";

char*ptr;

printf("destinationbeforememcpy:%s ",dest);

ptr=memcpy(dest,src,strlen(src));

if(ptr)

printf("destinationaftermemcpy:%s ",dest);

else

printf("memcpyfailed");

return0;

}

输出:

[cpp] view plain

/*************************************************************

destinationbeforememcpy:

destinationaftermemcpy:******************************456709

**************************************************************/

@函数名称:memccpy

函数原型:void *memccpy(void *dest, const void *src, int c, size_t n)

函数功能:字符串拷贝,到指定长度或遇到指定字符时停止拷贝

函数返回:

参数说明:src-源字符串指针,c-中止拷贝检查字符,n-长度,dest-拷贝底目的字符串指针

所属文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*src="Thisisthesourcestring";

chardest[50];

char*ptr;

ptr=memccpy(dest,src,'c',strlen(src));

if(ptr)

{

*ptr='';

printf("Thecharacterwasfound:%s",dest);

}

else

printf("Thecharacterwasn'tfound");

return0;

}

输出:

[cpp] view plain

/*****************************************

Thecharacterwasfound:Thisisthesourc

*****************************************/

PS:指定字符被复制到dest中,memccpy返回了dest中指定字符的下一处的地址,返回NULL表示未遇到指定字符

@函数名称:memchr

函数原型:void *memchr(const void *s, int c, size_t n)

函数功能:在字符串中第开始n个字符中寻找某个字符c的位置

函数返回:返回c的位置指针,返回NULL时表示未找到

参数说明:s-要搜索的字符串,c-要寻找的字符,n-指定长度

所属文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstr[17];

char*ptr;

strcpy(str,"Thisisastring");

ptr=memchr(str,'r',strlen(str));

if(ptr)

printf("Thecharacter'r'isatposition:%d",ptr-str);

else

printf("Thecharacterwasnotfound");

return0;

}

@函数名称:memcmp

函数原型:int memcmp(const void *s1, const void *s2,size_t n)

函数功能:按字典顺序比较两个串s1和s2的前n个字节

函数返回:<0,=0,>0分别表示s1<,=,>s2

参数说明:s1,s2-要比较的字符串,n-比较的长度

所属文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*buf1="ABCDE123";

char*buf2="abcde456";

intstat;

stat=memcmp(buf1,buf2,5);

printf("Thestringstoposition5are");

if(stat)printf("not");

printf("thesame ");

return0;

}

@函数名称:memicmp

函数原型:int memicmp(const void *s1, const void *s2, size_t n)

函数功能:按字典顺序、不考虑字母大小写对字符串s1,s2前n个字符比较

函数返回:<0,=0,>0分别表示s1<,=,>s2

参数说明:s1,s2-要比较的字符串,n-比较的长度

所属文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*buf1="ABCDE123";

char*buf2="abcde456";

intstat;

stat=memicmp(buf1,buf2,5);

printf("Thestringstoposition5are");

if(stat)printf("not");

printf("thesame");

return0;

}

输出:

[cpp] view plain

/**************************************

***************************************/

@函数名称:memmove

函数原型:void *memmove(void *dest, const void *src, size_t n)

函数功能:字符串拷贝

函数返回:指向dest的指针

参数说明:src-源字符串,n-拷贝的最大长度

所属文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

chardest[40]="";

printf("destinationpriortomemmove:%s ",dest);

memmove(dest+1,dest,35);

printf("destinationaftermemmove:%s",dest);

return0;

}

PS:与memcpy不同的是,memmove可以处理目的字符串与源字符串地址空间出现重叠的情况,可保证待复制的内容不被破坏。

@函数名称: memset

函数原型: void *memset(void *s, int c, size_t n)

函数功能: 字符串中的n个字节内容设置为c

函数返回:

参数说明: s-要设置的字符串,c-设置的内容,n-长度

所属文件: <string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

#include<mem.h>

intmain()

{

charbuffer[]="Helloworld";

printf("Bufferbeforememset:%s/n",buffer);

memset(buffer,'*',strlen(buffer)-1);

printf("Bufferaftermemset:%s",buffer);

return0;

}


c语言string的用法大全相关 文章 :

★ c语言string的用法

★ c语言的用法

★ Linux C语言字符与字符串处理

★ c语言中strcmp的用法

★ c语言大括号的用法

★ c语言位运算符的用法

★ c语言char的用法

★ c语言中sort的用法详解

★ c语言中int的用法

★ c语言map的用法

I. c语言中string的请输入文件名什么意思

c语言中string的请输入文件名意思是需要输入相应字符串。因为在c语言山培中string:是一个字符串函数,包含头文件"string.h"。请输入文件名:后面括号中需要输入相应字符串,达到代码运行的逗迟唯旦棚地步。所以c语言中string的请输入文件名意思是需要输入相应字符串。

J. c语言定义string是什么意思

在java、C#中,String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。 String 对象是System.Char对象的有序集合,用于表示字符串。

String对象的值是该有序集合的内容,并且该值是不可变的。

string用法举例:

string s = "abcdef";cout << s << endl

结果就应该是输出了字符'a',同时string类型也可以直接加上另一个字符串,例如

string a = "abc", b = "bcd";a = a + b;cout << a << endl

结果应该输出字符串“abcbcd”

而且string还支持直接比较两个字符串的大小(内置字典序比较方式)。

C语言是一门面向过程的计算机编程语言,与C++、C#、Java等面向对象编程语言有所不同。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、仅产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

C语言描述问题比汇编语言迅速、工作量小、可读性好、易于调试、修改和移植,而代码质量与汇编语言相当。C语言一般只比汇编语言代码生成的目标程序效率低10%-20%。因此,C语言可以编写系统软件。

当前阶段,在编程领域中,C语言的运用非常之多,它兼顾了高级语言和汇编语言的优点,相较于其它编程语言具有较大优势。计算机系统设计以及应用程序编写是C语言应用的两大领域。同时,C语言的普适较强,在许多计算机操作系统中都能够得到适用,且效率显着。