又忘了C++怎么输入带空格的一行字符串了.....
特地写下来,以免又忘了。(注意,cin后要加getchar(),不然后面的getline()会直接读取'\n')
#include <iostream> using namespace std; int main() { char s[27] = "VWXYZABCDEFGHIJKLMNOPQRSTU"; char start[11], end[4], a[300]; cin>>start; while (start[0] - 'S' == 0) { getchar(); cin.getline(a, 300); cin>>end; int len = strlen(a); for (int i=0; i<len; i++) if (a[i] - 'A' >= 0 && a[i] - 'Z' <=0) cout<<s[a[i]-'A']; else cout<<a[i]; cout<<endl; cin>>start; } }
char*gets(char*buffer);也可以。功能:从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。
如 HDOJ 1219 AC Me
#include <iostream> using namespace std; int main() { int i, temp; int a[26]; char s[100001]; while (gets(s)) { for (i=0; i<26; i++) a[i] = 0; for (i=0; s[i]; i++) { temp = s[i] - 'a'; if (temp >= 0) a[temp]++; } for (i=0; i<26; i++) cout<<char('a' + i)<<":"<<a[i]<<endl; cout<<endl; } }