• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

标准C++输入输出和字符串类学习小程序集锦

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

【声明】本博客内容可以随意转载,但请您注明出处: http://www.cnblogs.com/hanxi/

代码可能有很多不足之处或有错误,还望各路朋友们指点指点。

1.计算一个字符串在另一个字符串中出现的次数

程序思想:调用find函数查找子串,找到一个就记录一次,并接着往下找,直到找不到为止。

/*函数作用:用来计算一个字符串在另一个字符串中出现的次数*/

#include
<string>
using std::string;
int count_str(string maxstr, string minstr)
{
int count = 0;
int i = 0;
while ((i = maxstr.find(minstr,i)) != string::npos)
{
count
++;
i
+= minstr.size();
}
return count;
}

下面是一个测试程序:

#include <iostream>
using namespace std;
int count_str(string maxstr, string minstr);
int main()
{
string maxstr = "desdfdesdfdefgd";
string minstr = "de";
int num = count_str(maxstr, minstr);
cout
<< "出现了" << num << "" << endl;
}

编译过程和结果:

 $ g++ findstr.cpp main.cpp -o main

 $ ./main

出现了3次

2.接受月份的数字,返回这个月份的名字:

程序思想:使用字符串s存放1到12月的英文单词,每个单词用逗号隔开,首尾也加逗号,然后进入一个循环判断该月是那一个月,判断过程中,

j存储月份名字开始字母的位置,i存放该月份名字后面的逗号的位置,找到月份的单词后就退出循环,最后返回字串。

/*数字转月份的名字*/

#include
<string>
#include
<iostream>
using namespace std;

string monthname(int monthnum)
{
string s=",January,February,March,April,May,June,July,August,Spetember,October,November,December,";
int count = 0;
int i=0,j=0;
do
{
j
= i;//j存储月份名字开始字母的位置,i存放该月份名字后面的逗号的位置
i = s.find(",",i+1);
count
++;
}
while (count != monthnum);
return s.substr(j+1,i-j-1);
}

测试程序代码:

//monthnametext.cpp
#include <iostream>
using namespace std;
string monthname(int monthnum);
int main()
{
int num = 1;
for (;num<=12;num++)
{
cout
<< monthname(num) << endl;
}
}

编译过程和结果:

$ g++ monthname.cpp monthnametext.cpp -o m
$ .
/m

January
February
March
April
May
June
July
August
Spetember
October
November
December

3.接受月份的名字,返回这个月份的数字

程序思想: 使用字符串s存放1到12月的英文单词,每个单词用逗号隔开,首加逗号。然后在s中查找monthname,找到后记录第一个字母的位置。

再调用刚开始写的第一个函数count_str。

/*月份的名字转数字*/

#include
<string>
#include
<iostream>
using namespace std;
int count_str(string maxstr, string minstr);
int monthnum(string monthname)
{
string s=",January,February,March,April,May,June,July,August,Spetember,October,November,December";
int i=0;
i
= s.find(monthname);
return count_str(s.substr(0,i), ",");
}

测试程序的代码:

/monthnumtext.cpp
#include
<iostream>
using namespace std;
int monthnum(string monthname);
int main()
{
cout
<< monthnum("January") << endl;
cout
<< monthnum("February") << endl;
cout
<< monthnum("March") << endl;
cout
<< monthnum("April") << endl;
cout
<< monthnum("May") << endl;
cout
<< monthnum("June") << endl;
cout
<< monthnum("July") << endl;
cout
<< monthnum("August") << endl;
cout
<< monthnum("Spetember") << endl;
cout
<< monthnum("October") << endl;
cout
<< monthnum("November") << endl;
cout
<< monthnum("December") << endl;
}

编译过程和结果:

[hanxi@hanxi-workstation Documents]$ g++ findstr.cpp monthnum.cpp monthnumtext.cpp -o mo
[hanxi@hanxi
-workstation Documents]$ ./mo
1
2
3
4
5
6
7
8
9
10
11
12

4.接受含有大写和小写字母的字符串,返回这个字符串的全部是小写字母的拷贝或全是大写字母的拷贝

程序思想:若要得到小写字母,找出大写字母并将其改为小写字母。若要得到大写字母则相反。

#include <iostream>
#include
<string>
using namespace std;

bool tolower_or_toupper(string &str, char option)
{
int i = 0;
if (option != 'l' && option != 'u') //使用字符'l'和'u'判断是转换为小写还是大写字母
{
cout
<< option << endl;
cout
<< "The option is error! It must be 'l' or 'u'" << endl;
return false;
}
for (i = 0; i<str.length();i++)
{
if (isupper(str.at(i)) && option == 'l')//转换为小写的
{
str.at(i)
= str.at(i) + 'a'-'A';//ascii码记不住没关系,临时转换就是了
}
else if (islower(str.at(i)) && option == 'u')//转换为大写的
{
str.at(i)
= str.at(i) + 'A'-'a';
}
}
return true;
}

下面是一个测试程序:

#include <iostream>
#include
<string>
using namespace std;

bool tolower_or_toupper(string &str, char option);
int main()
{
string str = "aFkFHkKFNDJHDNFkfksdjjek";
tolower_or_toupper(str,
'l');//全部转换为小写的
cout
<< "The loer letters is : " << str << endl;
tolower_or_toupper(str,
'u');//全部转换为大写的
cout
<< "The upper letters is : " << str << endl;
return 1;
}

下面的也是一个测试程序,只不过嫁了个文件操作:

#include <iostream>
#include
<fstream>
#include
<string>
using namespace std;

bool tolower_or_toupper(string &str, char option);
int main()
{
string str = "aFkFHkKFNDJHDNFkfksdjjek";
tolower_or_toupper(str,
'l');
cout
<< "The loer letters is : " << str << endl;
str
= "aFkFHkKFNDJHDNFkfksdjjek";
tolower_or_toupper(str,
'u');
cout
<< "The upper letters is : " << str << endl;

//测试一个文件的更改
char buffer[256];
char delim;
fstream file;
file.open(
"tmp");
if (file.is_open())
{
file.
get(buffer,256, delim=EOF);
cout
<< buffer << endl;
file.close();
}
str.assign(buffer);
tolower_or_toupper(str,
'u');
cout
<< "The upper letters is : " << str << endl;
return 1;
}

编译和运行:

[hanxi@hanxi-workstation Documents]$ g++ letter.cpp testletter.cpp -o strtest
[hanxi@hanxi-workstation Documents]$ ./strtest
The loer letters
is : afkfhkkfndjhdnfkfksdjjek
The upper letters
is : AFKFHKKFNDJHDNFKFKSDJJEK
Walter
's goal in life was to become a successful surgeon. First, though, he had to get through high school, so he concentrated all his efforts on his studies —— in particular, biology, chemistry, and math. Because he worked constantly on these subject
The upper letters is : WALTER'S GOAL IN LIFE WAS TO BECOME A SUCCESSFUL SURGEON. FIRST, THOUGH, HE HAD TO GET THROUGH HIGH SCHOOL, SO HE CONCENTRATED ALL HIS EFFORTS ON HIS STUDIES —— IN PARTICULAR, BIOLOGY, CHEMISTRY, AND MATH. BECAUSE HE WORKED CONSTANTLY ON THESE SUBJECT

<还会继续更新>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序经济圈:日活4亿,覆盖超200个细分行业发布时间:2022-07-18
下一篇:
微信小程序,获取二维码发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap