现在的位置: 首页 > 综合 > 正文

string型字符串

2017年12月27日 ⁄ 综合 ⁄ 共 7383字 ⁄ 字号 评论关闭

1::char和string比较

#include <iostream>
#include <string>
using namespace std;
int main()
{
 string str="string类型字符串";
 char ch[]="char型字符串";
 cout<<str<<endl;
 cout<<ch<<endl;
 cout<<"请输入'狗'的英文单词"<<endl;
 cin>>str;
 if (str=="dog")
 {
  cout<<"狗:"<<str<<endl;
  cout<<str<<"的第1个字符是:"<<str[0]<<endl;
 }
 else
 {
  cout<<"输入错误!"<<endl;
 }
 cout<<"请输入'猪'的英文单词"<<endl;
 cin>>ch;
 if (strcmp(ch,"pig")==0)
  /*
   strcmp(str1,str2)
   如果str1<str2 返回小于0的数字
   如果str1=str2 返回0
   如果str1>str2 返回大于0的数字
  */
 {
  cout<<"猪:"<<ch<<endl;
  cout<<ch<<"的第1个字符是:"<<ch[0]<<endl;
 }
 else
 {
  cout<<"输入错误!"<<endl;
 }
 return 0;
}

2::string合并

#include <iostream>
#include <string>
using namespace std;

int main()
{
 string str1="Gave me ";
 string str2="a cup ";
 //str1+=str2;
 //str1.assign(str2,3,1);
 cout<<strlen(str1.c_str())<<endl;
 cout<<str1.size()<<endl;
 cout<<str1<<endl;
 cout<<str2<<endl;
 string str3;
 cout<<str3.length()<<endl;
 cout<<str3.size()<<endl;
 return 0;
}

 

3::append任意合并字符串中的字符

#include <iostream>
#include <string>
using namespace std;

int main()
{
 char ch1[10] = "ab";
 char ch2[] = "abcdef";
 strncat(ch1,ch2,3);
 cout<<ch1<<endl;
 string str1 = "ab";
 string str2 = "abcdefg";
 str1.append(str2,2,3);
 //append(需要的字符串,从哪个字符开始,复制几个)
 cout<<str1<<endl;
 return 0;
}

4::string替换replace

#include <iostream>
#include <string>
using namespace std;

int main()
{
/*
 char ch1[10] = "ef";
 char ch2[] = "abcdef";
 strncpy(ch1,ch2,3);
 cout<<ch1<<endl;
*/
 string str1 = "xz";
 string str2 = "abcdefghijklmn";
 str1.replace(0,2,str2,3,5);
 /*replace(起始,替换几个,从哪里替换,从哪块开始,替换几个)*/
 cout<<str1<<endl;

 string str3 = "xz";
 char str5='T';
 str3.replace(0,2,2,str5);
 /*replace(起始,替换几个,替换几次,从哪替换)*/
 cout<<str3<<endl;
 return 0;
}

5::拷贝字符串

#include <iostream>
#include <string>
using namespace std;

int main()
{
/*
 char ch1[15] = "abcdefghijklmn";
 char ch2[]   = "1234567890";
 cout<<"源字符串:"<<ch1<<endl;
 memmove(目的字符串,源字符串,拷贝的字符数目)
 memmove(ch1,ch2,10);
 cout<<"拷贝后 : "<<ch1<<endl;
*/
 string str1  = "abcdefghijklmn";
 char ch2[]   = "1234567890";
 cout<<"源字符串:"<<ch2<<endl;
 int n=str1.copy(ch2,10,0);
 cout<<"拷贝了 : "<<n<<"字符"<<endl;
 cout<<"拷贝后 : "<<ch2<<endl;
 return 0;
}

6::插入

#include <iostream>
#include <string>
using namespace std;

int main()
{
 string str1 = "01789";
 string str2 = "23456";
 str1.insert(2,str2,0,5);
 cout<<str1<<endl;
 return 0;
}

7::删除

#include <iostream>
#include <string>
using namespace std;

int main()
{
 string s("give me a cup");
 cout<<"原始数据值:"<<s<<endl;
 s.erase(2,2);
 /*erase(从哪个开始删除,删除几个)*/
 cout<<"现在数据值:"<<s<<endl;
 s.erase(2);
 /*从哪个位置开始删除,后面的字符全删除*/
 cout<<"现在数据值:"<<s<<endl;
 s.erase();
 /*从0位置开始删除,后面的字符全删除*/
 cout<<"现在数据值:"<<s<<endl;
 return 0;
}

8::查找

#include <iostream>
#include <string>
using namespace std;

int main()
{
/* char ch1[15];
 char *p,c='w';
 strcpy(ch1,"hello world");
 p=strchr(ch1,c);
 if (p)
 {
  cout<<"字符"<<c<<"位于第"<<p-ch1<<"位"<<endl;
 }
 else
 {
  cout<<"没有找到该字符!"<<endl;
 }*/
 string str1("hello worldw");
 int f=str1.rfind('w');
 /*
  str1.find_first_not_of('w',0)查找第一个与w不相等的字符
  str1.find_first_of('w',0)查找第一个与w相等的字符
  str1.find_last_of('w')查找最后一个与w相等的字符
  str1.find_last_not_of('w')查找最后一个与w不相等的字符
  str1.rfind('w')反向查找与w相同的字符
 */
 if (f!=string::npos)
 {
  cout<<"字符在第"<<f<<"位"<<endl;
 }
 else
 {
  cout<<"没有找到该字符!"<<endl;
 }
 return 0;

9::string字符串的比较

#include <iostream>
#include <string>
using namespace std;

int main()
{
 string s1="155";
 string s2="52";
 char c[]="34";
 int i,j,k,l,m,n;
 i=s1.compare(s2);
 /*s1与s2比较,返回0 相等,1为s1大于s2,-1为s1小于s2*/
 j=s2.compare(c);
 k=s1.compare(0,2,s2);/*从下标0开始中,依次取两个字符*/
 l=s1.compare(1,1,s2,0,1);/*compare(s1从下标为1开始,取一个字符,与哪个比较,从下标为几开始,取几个字符)*/
 m=s1.compare(1,1,c,0,1);
 n=s1.compare(1,1,c,1);
 cout<<s1<<":"<<s2<<"="<<i<<endl;
 cout<<s1<<":"<<s2<<"="<<j<<endl;
 cout<<s1[0]<<s1[1]<<":"<<s2<<"="<<k<<endl;
 cout<<s1[1]<<":"<<s2[0]<<"="<<l<<endl;
 cout<<s1[1]<<":"<<c[0]<<"="<<m<<endl;
 cout<<s1[1]<<":"<<c[0]<<"="<<n<<endl;
 return 0;
}

10::判断string为空的empty

#include <iostream>
#include <string>
using namespace std;

int main()
{
 string str="";
 if (str.empty())
 {
  cout<<"字符串为空!"<<endl;
 }
 else
 {
  cout<<str<<endl;
 }
 return 0;
}

11::交换两数据位置swap

#include <iostream>
#include <string>
using namespace std;

int main()
{
 char ch1[15]="ofru";
 char ch2[15]="";
 swab(ch1,ch2,strlen(ch1));
 cout<<ch1<<endl;
 cout<<ch2<<endl;
 string s1="ofru";
 string s2="";
 s1.swap(s2);
 cout<<s1<<endl;
 cout<<s2<<endl;
 return 0;
}

 12::把string转换成char
#include <iostream>
#include <string>
using namespace std;

int main()
{
 string str="hello world";
 const char *ch;
 ch=str.c_str();
 cout<<ch<<endl;
 return 0;
}
13::Char型字符串与函数
#include <iostream>
#include <string>
using namespace std;
int get(const char*p);
void main()
{
 char ch[15]="hello world";
 char *p="very well";
 int a=get(ch);
 int b=get(p);
 cout<<"指针的第1个元素的值:"<<*(p+0)<<endl;
 cout<<"指针的第2个元素的值:"<<*(p+1)<<endl;
 cout<<ch<<"共有"<<a<<"个字符"<<endl;
 cout<<p<<"共有"<<b<<"个字符"<<endl;
}
int get(const char *p)
{
 int count=0;
 while(*p)
 {
  count++;
  p++;
 }
 return count;
}
14::函数如何返回字符串
#include <iostream>
#include <string>
using namespace std;
char *get(char*str);
void main()
{
 char c[10];
 char *ch;
 cout<<"请输入您的名字:";
 cin>>c;
 ch=get(c);
 cout<<"您的名字是:"<<ch<<endl;
 delete []ch;
 ch=get("Jake");
 cout<<"您的名字是:"<<ch<<endl;
 delete []ch;
 char *ch1="Make";
 ch=get(ch1);
 cout<<"您的名字是:"<<ch<<endl;
 delete []ch;
}
char*get(char*str)
{
 char *p=new char[strlen(str)+1];
 strcpy(p,str);
 cout<<p;
 return p;
}
15::结构体
#include <iostream>
#include <string>
using namespace std;
struct people
{
 int age;
 double weight;
 double tall;
 char *name;
 char *native;
 bool sex;
};
void check(bool s){
 if (s==1)
 {
  cout<<"男"<<endl;
 }
 else
 {
  cout<<"女"<<endl;
 }
}
void main()
{
 people jake=
 {
   34,
   179.3,
   180.5,
   "Jake",
   "齐市",
   1
 };
 cout<<jake.name<<endl;
 cout<<jake.native<<endl;
 cout<<jake.tall<<endl;
 cout<<jake.weight<<endl;
 cout<<jake.age<<endl;
 check(jake.sex);
}
16::结构体与构造函数
#include <iostream>
#include <string>
using namespace std;
struct people
{
 people(double t_weight,double t_tall,int t_age,string t_name,string t_native,bool t_sex);
 int age;
 double weight;
 double tall;
 string name;
 string native;
 bool sex;
};
void check(bool s){
 if (s==1)
 {
  cout<<"男"<<endl;
 }
 else
 {
  cout<<"女"<<endl;
 }
}
void main()
{
 people jake(115.5,175.3,25,"Gui","齐市",1);
 cout<<jake.name<<endl;
 cout<<jake.native<<endl;
 cout<<jake.tall<<endl;
 cout<<jake.weight<<endl;
 cout<<jake.age<<endl;
 check(jake.sex);
}
people::people(double t_weight,double t_tall,int t_age,string t_name,string t_native,bool t_sex)
{

 weight=t_weight;
 tall=t_tall;
 age=t_age;
 name=t_name;
 native=t_native;
 sex=t_sex;
}
17::两个相同的结构体的赋值
#include <iostream>
#include <string>
using namespace std;
struct people
{
 double weight;
 double tall;
};
void main()
{
 people Make={115.5,175.5};
 people Jake={130.6,170.1};
 Make=Jake;
 cout<<Make.tall<<"/t"<<Jake.tall<<endl;
 cout<<Make.weight<<"/t"<<Jake.weight<<endl;
}
18::结构体与函数
#include <iostream>
#include <string>
using namespace std;
struct time
{
 int hour;
 int minute;
};
const int perhour=60;
time *sum(time t1,time t2);
void show(time t){cout<<t.hour<<":"<<t.minute<<endl;}
void main()
{
 time one={8,15};
 time two={6,55};
 time *day=sum(one,two);
 cout<<"两天时间总计:";
 show(*day);
 time day3={9,35};
 cout<<"三天时间总计";
 time *p=sum(*day,day3);
 show(*p);
 delete day;
 delete p;
}
time *sum(time t1,time t2)
{
 time *total=new time;
 total->minute=(t1.minute+t2.minute)%perhour;
 total->hour=t1.hour+t2.hour+(t1.hour+t2.hour)/perhour;
 return total;
}
19::结构体与string
#include <iostream>
#include <string>
using namespace std;
const string & show(const string &p)
{
 cout<<p<<endl;
 return p;
}
void main()
{
 string str="hello world";
 string str1=show(str);
 cout<<str1<<endl;
}
20::数组与string
#include <iostream>
#include <string>
using namespace std;
void show(const string str[],int n);
void main()
{
 const int length=5;
 string str[length];
 for (int i=0;i<length;i++)
 {
  cout<<i+1<<":";
  cin>>str[i];
 }
 show(str,length);
}
void show(const string str[],int n)
{
 for (int i=0;i<n;i++)
 {
  cout<<i+1<<":"<<str[i]<<endl;
 }
}

【上篇】
【下篇】

抱歉!评论已关闭.