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.

猜你喜欢

转载自blog.csdn.net/yanyanwenmeng/article/details/81106495