C++ ->Chapter3 标准库类型之string 类型

一、代码 :LibType_String.cc

   主要熟悉标准库string类型的初始化、定义、常见的操作等。

#include <iostream>
#include <cctype>
using namespace std;
int main ()
{
    /*1.define and intilatize a string object*/
    string s1;               // default Constructor
    cout<<"s1-->"+s1<<endl;
    string s2(s1);
    cout<<"s2-->"+s2<<endl;
    string s3("hello");
    cout<<"s3-->"+s3<<endl;
    string s4(3,'a');
    cout<<"s4-->"+s4<<endl;

    /*2.read and write a string object*/
    /*************************************************************
      *read and ignore all blank characters(e.g.space,newline,tabs)
      *read characters until encounter space,and stop 
    **************************************************************/
    string s;
    cin>>s;                 //read whitespace-separated into s;
    cout<<s<<endl;
    cin.clear();
    cin.sync();
    string s5,s6;
    cin>>s5>>s6;
    cout<<"s5-->"+s5<<" s6-->"+s6<<endl;

    /*3.read full line text,use getline*/
        string line;
    //read line at time untile end-of-file
    while(getline(cin,line))
        cout<<line<<endl;

    /*4.operations of a string object*/
    string s7("hello");
    string s8("world!");
    cout<<"the length of s7 is "+s7.size()<<endl;
    cout<<"is s8 empty"<<s8.empty()<<endl;
    cout<<"second character in s7 is "+ s7[1]<<endl;
    bool b = true;
    b = s7!=s8;
    cout<<"result of comparing s7 with s8-> "+s7<<b<<endl;
    b = s7==s8;

    cout<<"result of comparing s7 with s8-> "<<b<<endl;
    b = s7>=s8;
    cout<<"result of comparing s7 with s8 ->"<<b<<endl;
    b= s7<=s8;
    cout<<"result of comparing s7 with s8 ->"<<b<<endl;
    b = s7>s8;
    cout<<"result of comparing s7 with s8 ->"<<b<<endl;
    b = s7<s8;
    cout<<"result of comparing s7 with s8 ->"<<b<<endl;
    b=isalpha('a');
        cout<<"a is or not a alpha->"<<b<<endl;
    return 0;
    
}

二、编译、连接、运行结果:

三、string对象中字符处理函数

     这些函数包含在cctype头文件中,如下:

  • isalnum(c) 如果c是字母或数字,则为true。
  • isalpha(c) 如果c是字母,则为true。
  • iscntrl(c) 如果c是控制字符,则为true。
  • isdigit(c) 如果c是数字,则为true。
  • isgraph(c) 如果c不是空格,但可以打印,则为true。
  • islower(c) 如果c是小写字母,则为true。
  • isaprint(c) 如果c是可打印字符,则为true。
  • ispunct(c) 如果c是标点符号,则为true。
  • isspace(c) 如果c是空白字符,则为true。
  • isupper(c) 如果c是大写字母,则为true。
  • isxdigit(c) 如果c是十六进制数,则为true。
  • tolower(c) 如果c是大写字母,则返回其小写形式,否则直接返回c。
  • toupper(c) 如果c是小写字母,则返回其大写形式,否则直接返回c。 

转载于:https://www.cnblogs.com/mizhongqin/archive/2013/03/23/C_plus_plus_StandardLibraryType_string.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值