字符变量,字符数组,string类

字符变量基本操作

//字符变量基本操作 
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
	char a , b , c;
	cin >> a;
	cout << a;
	scanf("%c" , &b);
	printf("%c" , b);
	c = getchar();
	putchar(c);
	return 0;
}

字符数组基本操作

//字符数组基本操作
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main()
{
	char a[20] , b[20] , c[20];
	cin >> a;
	cout << a << endl;
	cout << &a[2] << endl;
	printf("\n+++++++++++++\n");
	scanf("%s" , &b);
	printf("%s\n" , b);
	printf("%s" , &b[2]);
	printf("\n+++++++++++++\n");
	c[0] = getchar();
	putchar(c[0]);
	putchar(c[1]);
	printf("\n+++++++++++++\n");
	//字符数组比较函数
	//相等为0,a大为正数,a小为负数 
	if ( !strcmp(a , b) )
		printf("相等");
	else if ( strcmp(a , b) < 0 )
			printf("a的ascll码小");
	else if ( strcmp(a , b) > 0 )
			printf("a的ascll码大"); 
	return 0;
} 

循环输入字符数组

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main()
{
	char a[20] , i = 0 , b;
/*	for ( int i = 0 ; i < 5 ; i++ )
	{
		a[i] = getchar();
	}
	while ( (b = getchar() ) != '\n' )
	{
		a[i] = b;
		printf("55%c\n" , b);
		i++;
	}*/
	while (1)
	{
		b = getchar();
		if( b == '\n' )
			break;
		a[i] = b;
		i++;
	}
	printf("5%s" , a);
	return 0;
}

string类基本操作

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
	string s;
	string s1;
	int leg = 0;
	cin >> s;
	leg = s.length();
	cout << leg << endl;
	cout << s << endl;
	cin >> s1;
	cout << s1 << endl;
	s.append(s1);
	cout << s << endl; 
	s.insert(4 , " ");
	cout << s << endl;
	//不推荐的方法,用C语法操作string类 
	s.resize(20); //给scanf开内存空间 
	scanf("%s" , &s[0]);
	printf(s.c_str());  //把string类强制转化为C字符串输出 
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值