C++学习点滴
最近加入一个C++的学习群,群里免不了有些网友提问题。我也正好学习一下。把一些问题,一些小程序记录下来,让自己的C++水平慢慢提上来......
函数功能:
把输入的字符串中的标点符号去掉之后输出来,循环执行
如果输入的字符串没有标点符号,他就输出Nopunctuation character in the string ?!
1#include <string>
2#include <cctype>
3
4int main()
5{
6 string s,result_str;
7 bool has_punct;
8 char ch;
9 cout<<"Enter a string: "<<endl;
10 while(getline(cin,s))
11 {
12 has_punct=false;
13 result_str = "";
14 for(string::size_type index=0;index!=s.size();index++)
15 {
16 ch=s[index];
17 if(ispunct(ch))
18 {
19 has_punct=true;
20 }
21 else
22 result_str+=ch;
23 }
24 if(has_punct)
25 cout<<"Result: "<<endl<<result_str<<endl;
26 else
27 {
28 cout<<"Nopunctuation character in the string ?!"<<endl;
29 //return -1;
30 continue;
31 }
32
33 }
34 return 0;
35}
36
2#include <cctype>
3
4int main()
5{
6 string s,result_str;
7 bool has_punct;
8 char ch;
9 cout<<"Enter a string: "<<endl;
10 while(getline(cin,s))
11 {
12 has_punct=false;
13 result_str = "";
14 for(string::size_type index=0;index!=s.size();index++)
15 {
16 ch=s[index];
17 if(ispunct(ch))
18 {
19 has_punct=true;
20 }
21 else
22 result_str+=ch;
23 }
24 if(has_punct)
25 cout<<"Result: "<<endl<<result_str<<endl;
26 else
27 {
28 cout<<"Nopunctuation character in the string ?!"<<endl;
29 //return -1;
30 continue;
31 }
32
33 }
34 return 0;
35}
36