C++第五次作业

文章目录:

一:C++统计一段英文文本中英文单词的个数(主函数中实现功能)

代码实现

运行结果

二:C++用一个string类型的变量(准确来讲,称为对象)s存储一大段中文或英文文本,实现在其中查找并统计某词语t出现的次数。基本算法思想(使用数组下标访问的方法:通过下标的线性移动来访问字符串的各个字符)统计某单词例如”the”在文中出现的频率

方法一:通过下标的线性移动来访问字符串的各个字符

 代码实现

运行结果

方法二:直接用函数

运行结果

三:C++在文本中查找某个字词并替换为另一个字词

方法一:直接利用函数

代码实现

运行结果 

方法二:使用数组下标访问的方法通过下标的线性移动来访问字符串的各个字符


一:C++统计一段英文文本中英文单词的个数(主函数中实现功能)

	char s[]=	"Space, \"tab\", linefeed, carriage-return, formfeed, vertical-tab, and newline "
	"characters are called \"white-space characters\" because they serve the same "
	"purpose as the spaces between words and lines on a printed page --- they make "
	"reading easier. Tokens are delimited (bounded) by white-space characters and by "
	"other tokens, such as operators and punctuation. When parsing code, the C compiler "
	"ignores white-space characters unless you use them as separators or as components "
	"of character constants or string literals. Use white-space characters to make a program "
	"more readable. Note that the compiler also treats comments as white space Yirenxp.";

代码实现

#include <iostream> 
#include <cmath>
#include <cstring>
#include <ctype.h> 
using namespace std; 


int main()
{
	
	int i=0;//通过数组下标访问字符
	bool readalpha=0;//表示读到的是否是字符
	unsigned words=0;//统计单词个数
	
	char s[1000]="Space, \"tab\", linefeed, carriage-return, formfeed, vertical-tab, and newline "
	"characters are called \"white-space characters\" because they serve the same "
	"purpose as the spaces between words and lines on a printed page --- they make "
	"reading easier. Tokens are delimited (bounded) by white-space characters and by "
	"other tokens, such as operators and punctuation. When parsing code, the C compiler "
	"ignores white-space characters unless you use them as separators or as components "
	"of character constants or string literals. Use white-space characters to make a program "
	"more readable. Note that the compiler also treats comments as white space Yirenxp.";


	for (i = 0; i < 1000; i++)
	{
		if (  isalpha( s[i] ))
		{
			readalpha=1;
			words++;
		}else
		{
			 readalpha=0;
		}
	}
	cout <<"单词的个数为:"<<words<<endl;
	system("pause");
    return 0;
}


运行结果

二:C++用一个string类型的变量(准确来讲,称为对象)s存储一大段中文或英文文本,实现在其中查找并统计某词语t出现的次数。基本算法思想(使用数组下标访问的方法:通过下标的线性移动来访问字符串的各个字符)统计某单词例如”the”在文中出现的频率

方法一:通过下标的线性移动来访问字符串的各个字符

  代码实现

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
 
int main(){	
	string s="As promised in the first edition of this book, C++ has been evolving to meet "
			"the needs of its users.This evolution has been guided by the experience of users of widely "
			"varying backgrounds working in a great range of application areas. The C++ user community "
			"has grown a hundredfold during the six years since the first edition of this book; many "
			"lessons have been learned, and many techniques have been discovered and/or validated "
			"by experience. Some of these experiences are reflected here. The primary aim of the "
			"language extensions made in the last six years has been to enhance C++ as a language "
			"for data abstraction and objectoriented programming in general and to enhance it as a tool "
			"for writing highquality libraries of user defined types in particular. A \"highquality library,\" is "
			"a library that provides a concept to a user in the form of one or more classes that are "
			"convenient, safe, and efficient to use. In this context, safe means that a class provides "
			"a specific typesafe interface between the users of the library and its providers; efficient "
			"means that use of the class does not impose significant overheads in runtime or space on "
			"the user compared with handwritten C wang code.";
 
	string t="the";
 
	bool flag=0; 
	unsigned count=0;
	int i_s=0,i_a=0,i_t=0;
 
	do
	{	
		i_a = i_s;						// i_a为辅助,最初跟随i_s
		i_t =0;							// i_t作为访问t串字符的下标,先指向其首部
		
		do
		{

				if(tolower(s[i_a]==tolower(t[i_t])))		//此处还要考虑大小写的问题,思考怎么改?
				//if(s[i_a]==t[i_t])
				{	
					flag=1;				//bool类型的flag,若对应字符相等则赋值为1
					i_a++ ;	
					i_t ++;
				}
				else					//对应字符不等,说明不匹配,跳出内层do-while
				{	
					flag=0;
					break;
				}
		} while (i_t <t.length());
 
		//内层循环结束后判断flag为何值
			if(flag==1)
			{	
				count++;				//count统计t出现的次数
				i_s = i_s +t.length();	//i_s下移一个t串长度
			}else
			{
				i_s ++;
			}
	}while(i_s < (s.length()-t.length()));//i_s还没走到s尾部,为什么要减//掉一个t.length()?
 
 
	 cout<<"单词the"<<"出现的次数为:"<<count<<"次"<<endl;
	system("pause");
	return 0;
}
 

运行结果

方法二:直接用函数

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

 

int main()

{

            int n=0,sum=0;

            string s1("As promised in the first edition of this book, C++ has been evolving to meet "
			"the needs of its users.This evolution has been guided by the experience of users of widely "
			"varying backgrounds working in a great range of application areas. The C++ user community "
			"has grown a hundredfold during the six years since the first edition of this book; many "
			"lessons have been learned, and many techniques have been discovered and/or validated "
			"by experience. Some of these experiences are reflected here. The primary aim of the "
			"language extensions made in the last six years has been to enhance C++ as a language "
			"for data abstraction and objectoriented programming in general and to enhance it as a tool "
			"for writing highquality libraries of user defined types in particular. A \"highquality library,\" is "
			"a library that provides a concept to a user in the form of one or more classes that are "
			"convenient, safe, and efficient to use. In this context, safe means that a class provides "
			"a specific typesafe interface between the users of the library and its providers; efficient "
			"means that use of the class does not impose significant overheads in runtime or space on "
			"the user compared with handwritten C wang code. ");

            string s2("the");

            string temp=s1;

            while (1){ //循环:查找到子串就把子串删去

                        n=temp.find(s2);//返回子串的位置

                        if (n!=-1)//n=-1表示未找到子串
						{ 
							//开一个新的字符串变量temp存储删去子串后的字符串
                             temp=temp.substr(n+s2.length(),temp.length()-s2.length());   

                             sum++;//出现次数
                        }
						else
						{
							break;
						}
            }

            cout<<"单词"<<s2<<"出现的次数为:"<<sum<<"次"<<endl;
			system("pause");
			return 0;

}

运行结果

三:C++在文本中查找某个字词并替换为另一个字词

方法一:直接利用函数

代码实现

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

 
int main()
{
    
	int p;//保存找到的字符位置
	string str,world;//保存要查找的字符串,被查找的字符串
 
	cout << "请输入一个字符串:"<<endl;
	cin>>str;
	cout <<"请输入要替换的字符串:"<<endl;
	cin>>world;
 
	//查找单词在字符串中的起始位置
	p=str.find(world);//保存在字符变量p中
	if (p==-1)
	{
		cout << "抱歉,字符串中没有你想要查找的字符串:"<<endl;
	}
	while  (p!=-1)
	{
		str.replace(p,world.size(),"刘鑫磊");
		p=str.find(world);
	}
	
	cout <<str<<endl;
	system("pause");
    return 0;
}
 

运行结果 

 

方法二:使用数组下标访问的方法通过下标的线性移动来访问字符串的各个字符

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页