7.3 字符串3 (字符串库函数, 用strlen遍历字符数组的正确用法 )

#include<iostream>
#include<cstring>
using namespace std;
char a[10];
int main()
{
	gets(a);
	cout << strlen(a) << endl;
	return 0;
}

   、'\0'不计算进去

#include<iostream>
#include<cstring>
using namespace std;
void PrintSmall(char s1[], char s2[])//输出字典序小的字符串 
{
	if(strcmp(s1, s2) <= 0) 
		cout << s1;
	else
		cout << s2;		
}

int main()
{
	char s1[30];
	char s2[30];
	char s3[30];
	strcpy( s1, "Hello");//s1 = Hello
	strcpy( s2, s1);//s2 = Hello
	cout << "1) " << s2 << endl;//1) Hello
	strcat( s1, ",world");//Hello,world 
	cout << "2) " << s1 << endl;//2) Hello,world
	cout << "3) "; PrintSmall("abc", s2); cout << endl;//3) Hello
	cout << "4) "; PrintSmall("abc", "aaa"); cout << endl;//4) aaa
	int n = strlen(s2);//5
	cout << "5) " << n << "," << strlen("abc") << endl;//5) 5,3
	strupr(s1);//HELLO,WORLD
	cout << "6) " << s1 << endl;//6) HELLO,WORLD
	return 0;
} 

s[i] 为0时终止循环,s[i]='\0'时s[i]=0.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值