字符串读取及输出
第一次写博客,水平有限,望各路大佬指点指点!
本文主要是供我自己去学习!
获取单个字符串方法:
1. 将字符串逐个读取。
char c;
c = getchar();
如 题目 UVA 272
Input
"To be or not to be," quoth the Bard, "thatis the question".The programming contestant replied: "I must disagree.To `C' or not to `C', that is The Question!"
Output
``To be or not to be,'' quoth the Bard, ``thatis the question''.The programming contestant replied: ``I must disagree.To `C' or not to `C', that is The Question!''
代码如下
2.getch()的用法
其头文件为conio.h
用ch=getch();会等待你按下任意键之后,把该键字符所对应的ASCII码赋给ch
输入字符‘a’ 但是不会显示在控制台,按上述程序结果一次输出: 97,'a','a';
2.整行字符串获取
1.字符数组
char a[n];
cin>>a; 或者 scanf("%s",a);
cout<<a[i]; 或者 printf("%c",a[i]);
逐个字符输入输出(%c),整个字符串一次输入输出(%s),输出的字符中不包括结束符’\0’,用%s输出字符串时,printf函数中的输出项是字符数组名,不是数组元素名。
还有一种简单的用法:char a[] = {"abcdefg123456789hijklmn"};
2.字符串
string a;
此方法只能获取一段字符,而不能获取空格。
输入cin>>a; 输出 cout<<a;
while(cin>>a); 当连续输入时,会替换之前的a。
结果
3.getline()函数用法
水平有限,望各位大佬指点迷津。希望我的整理能对大家起到一定的作用。