C++字符串-输出字符串的每个字符

实例1:输出字符串的每个字符

1.循环方式1

int main()
{
	char ch[10] = { 'a','b','s','c','d','w','j','e','o','w' };  //字符数组
	for (int i = 0; i < 10; i++)
	{
		cout << ch[i] << endl;
	}
	return 0;
}

2.循环方式2

int main()
{
	char ch[10] = { 'a','b','s','c','d','w','j','e','o','w' };  //字符数组
	for (char a:ch)
	{
		cout << a << endl;
	}
	return 0;
}

3.综合

#include
using namespace std;

int main()
{
	string res = "abcdef";
	char ch[10] = { 'a','b','s','c','d','w','j','e','o','w' };  //字符数组
	char aa[10] = "abcdefghi";//实际只有9个字符,留一个位置给最后一个/0表示结束
	cout << "输出字符数组中的字符:" << endl;
	for (char a:ch)
	{
		cout << a << endl;
	}
	cout << "输出字符串中的字符:" << endl;
	for (char b : res)
	{
		cout << b << endl;
	}
	cout << "输出用字符串初始化的字符数组中的字符:" << endl;
	for (char c : aa)
	{
		cout << c << endl;
	}
	return 0;
}

C++字符串-输出字符串的每个字符_第1张图片

你可能感兴趣的:(C++基础知识,c++)