strcat()函数与strlen()函数与strcpy()的使用

trcat()函数与strlen()函数(7.21)

#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
    char str1[100], str2[100];
    cin>>str1;
    cin>>str2;
    //cout<<str1<<"\n";
    //cout<<str2<<"\n";
    strcat (str1 , str2);               //连接函数把str2加在str1的后边;
    cout<<str1<<endl;

    cout<<strlen (str1)<<endl;          //计算str1字符串的长度!不含结尾的"\0";若输入"Hello world"那么值为5,只测空格前边的字符长度!
    cout<<sizeof (str1);                   //测得的实际长度还是str1[100]值为100,不是str1的输入值! 

    return 0;

}


#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
    char name[100];         //当定义了一个字符数组,用于存放字符串,若一开始没有初始化那么要么用cin>>地址要么用strcpy()函数;
    char *p = "123";            
    strcpy (name , p);    //strcpy( 目标数组或字符串, 源字符串 )的使用形式!
    cout<<name<<"\n";
    cout<<p<<"\n";

    return 0;
}

总结:
    1.strcpy( 目标数组或字符串, 源字符串 )的使用形式!
    2.赋值时连同源字符串的末尾'\0'也被复制过去。
    3.目标数组或字符串 > 源字符串 只有这样才能有足够的空间容纳源字符串的数值!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值