博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
标准C++输入输出和字符串类学习小程序集锦
阅读量:5093 次
发布时间:2019-06-13

本文共 5426 字,大约阅读时间需要 18 分钟。

【声明】本博客内容可以随意转载,但请您注明出处:

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

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

<还会继续更新>

转载于:https://www.cnblogs.com/hanxi/archive/2011/06/12/2078649.html

你可能感兴趣的文章
pyspark 逻辑回归程序
查看>>
docker swarm 常用命令
查看>>
毕业三年,同学基本都辞职了,大部分人看完很有同感~
查看>>
AJAX异步交互
查看>>
在django项目中使用django-ckeditor
查看>>
[Python3] 017 字典的内置方法
查看>>
listView中的button控件获取索引
查看>>
读取properties配置文件的方法
查看>>
es6语法图片切换demo
查看>>
Spring基础系列-AOP源码分析
查看>>
XML1_XML基础
查看>>
结构体、共用体
查看>>
找到IIS服务
查看>>
命令行解决mysql中文乱码
查看>>
C#代码执行中等待10秒
查看>>
window下nodejs环境和express插件的安装
查看>>
leetcode刷题正则表达式
查看>>
杨辉三角模板
查看>>
通过 PackageManager 获得你想要的 App 信息
查看>>
楼市捞金术:我是怎么在9个月把房价炒高两三倍的
查看>>