字符串的输入输需要注意的点。UVA—10815,CSU—2080

首先我们并没有为 字符串定义专门的数据类型,而是把它存储于char数组里面,字符串中的字符依旧是存放在在相邻的存储单元,每个字符占据一个单元。一般字符数组里面的最后一个位置显示字符\0,这个字符就是空字符,用它来标记字符串的结束,它不是数字0,是非打印字符。也意味着数组的单元数必须比要存储的字符数多1。

(1)cin来读入字符串。形如:cin>>s;

   cin可以用来读string定义的字符串也可以读char a[maxn]数组。它使用空白(空格,制表符,换行符(通过回车输入))来确定字符串的结束位置。这说明通过cin读入字符数组时只能读入一个单词,之后的东西还是保存着在流中。cincin将该字符串放在数组中,并自动在结尾加入空字符\0。

(2)scanf来读入字符串,形如:scanf("%s",s);

  scanf的基本用法和cin一样,都是使用空白(空格,制表符,换行符(通过回车输入))来确定字符串的结束位置,但是它不能用来读string定义的字符串,如string s;scanf("%s",s);就不能运行。要想用scanf读入需要先预分配空间。像这样

(3)getchar来读入字符 形如:char ch=getchar();

getchar() 是从控制台接收字符,只会接收一个字符,包括空格、回车和Tab ,所以常用它来读入文本,结束标志为~Z,在这里我们需要在键盘输入信息时, 并不是在键盘上敲一个字符,该字符就立即送到计算机中,这些字符暂存在键盘的缓冲区中,只有按了Enter键才会把这些字符一起送到计算机中,然后按照先后顺序赋值给相应的变量。如果想要消去回车可以使用下面的语句清除回车:   while(getchar()!='\n');   这样就不会读入回车了。

如UVA—10815那题附ac代码,在这里每次输入一个单词,就放到set集合,利用set的自动排序来做。

#include<iostream>   
#include<string>   
#include<set>  
using namespace std;
set<string> a;
int main()
{
string s;
int i, j, k;
char c; s = "";
while ((c = getchar()) != EOF)
{
if (c >= 'A'&&c <= 'Z')
{
c += 32;
s += c;
}
else if (c >= 'a'&&c <= 'z')
{
s += c;
}
else
{
if (s != "")
{
a.insert(s);
}
s = "";
}
}
for (set<string>::iterator it = a.begin(); it != a.end(); ++it)
cout << *it << endl;
a.clear();
return 0;
}

(4)getline来读入整行,如:cin.getline(name);或cin.getline(name,20);或getline(cin,s);

getline()函数读取整行,它使用通过回车键输入的换行符来确定输入结尾,但不保存换行字符,可以读入空格。要调用这种方法,该函数有两个参数。第一个参数是用来储存输入行的数组名称,第二个参数是要读取的字符数。如果这参数为20,则函数最多读取19个字符,余下的空间用于储存自动在结尾处添加的空字符。getline()成员函数在读取指定数目的字符或者遇到换行符时停止读取。用第三种形式不需要确定读入的字符数。

 (5)用stringstream流读入文本。

   就如上面那题用流的ac代码:

 #include <iostream>
 #include <cstring>
 #include <cstdio>
 #include <algorithm>
 #include <sstream>
 #include <vector>
 #include <set>
 using namespace std;
 set<string> arr;
 int main()
 {
string str, re;
while (cin >> str)
{
int num = 0;
int len = str.length();
for (int i = 0; i < len; i++)
{
if (isalpha(str[i]))
{
str[i] = tolower(str[i]);
}
else
str[i] = ' ';
}
stringstream ss(str);
while (ss >> re)
arr.insert(re);
}
for (set<string>::iterator it = arr.begin(); it != arr.end(); it++)
{
cout << *it << endl;
}
return 0;
 }

  再如CSU—2580的那题用流读入文本。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <sstream>
#include <set>
#include <queue>
using namespace std;
const int maxn = 1005;
string s;
int a[26];
int b[100005];
queue<string>v;
char c3[100005];
char c1[26];
char c2[26];
int main()
{
for (int i = 0; i<26; i++)
{
c1[i] = 'a' + i;
c2[i] = 'A' + i;
}
string buf;
int cnt1 = 0;
while (cin >> s)
{
memset(a, 0, sizeof(a));
int len = s.length();
//cout<<len<<endl;
for (int i = 0; i<len; i++)
{
if (s[i] >= 'A'&&s[i] <= 'Z')
{
a[s[i] - 'A']++;
b[cnt1++] = s[i] - 'A';
}
if (s[i] >= 'a'&&s[i] <= 'z')
{
a[s[i] - 'a']++;
b[cnt1++] = s[i] - 'a';
}
}
stringstream ss(s);//读入的关键
while (ss >> buf)
{
v.push(buf);
}
}
//cout<<v.size()<<endl;
int max1 = 0;
int flag;
for (int i = 0; i<26; i++)
{
if (a[i]>max1)
{
max1 = a[i];
flag = i;
}
}
int cnt;
cnt = 30 - flag;
for (int i = 0; i<cnt1; i++)
{
b[i] = (b[i] + cnt) % 26;
}
//cout<<cnt<<endl;
int j = 0;
while (!v.empty())
{
string s1 = v.front();
v.pop();
//cout<<s1.length()<<endl;
for (int i = 0; i<s1.length(); i++)
{
if (s1[i] >= 'A'&&s1[i] <= 'Z')
{
cout << c2[b[j++]];
}
if (s1[i] >= 'a'&&s1[i] <= 'z')
{
cout << c1[b[j++]];
}
if (s1[i] == '`')
cout << " ";
if (s1[i] == '(')
cout << ",";
if (s1[i] == '}')
cout << ".";
}
cout << endl;
}
cout << endl;
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值