c++中一次读入一整行和一个词,比较两个字符串的大小和长度

 
 
下面代码实现从标准输入中读入一整行,读入一个词
    两个字符串的比较

 
 
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
void readOneLine();
void readOneWord();
void bigString(string, string);
string isEqualStrng(string, string);

int main()
{
	//readOneWord();
	string s1("Beijing");
	string s2("Shanghai");
	//bigString(s1, s2);
	cout<< isEqualStrng(s1, s2);
    return 0;
}

void readOneLine()     //P81->3.2 从标准输入中一次读入一整行
{
	string line;
	while (getline(cin, line))
		cout << line << endl;
}
void readOneWord()		//P81->3.2 从标准输入中一次读入一个词
{
	string word;
	while (cin >> word)
		cout << word << endl;
}

void bigString(string s1, string s2)  //P81->3.4 比较两个字符的大小
{
	if (s1 == s2)
		cout <<"\""<< s1 << "\"" << " == " << "\"" << s2 << "\"" << endl;
	else if (s1 > s2)
		cout << "\"" << s1 << "\"" << " > " << "\"" << s2 << "\"" << endl;
	else
		cout << "\"" << s2 << "\"" << " > " << "\"" << s1 << "\"" << endl;
}

string isEqualStrng(string s1, string s2)	//P81->3.4 比较两个字符的长度
{
	if (s1.size() == s2.size())
	{
		cout << "The two string's size is equal!" << endl;
		return 0;
	}
	else if (s1.size() > s2.size())
	{
		cout << "The longer size of strings is " << "\"" << s1 << "\"" << endl;
		return s1;
	}
	else
	{
		cout << "The longer size of strings is " << "\"" << s2 << "\"" << endl;
		return s2;
	}

}

猜你喜欢

转载自blog.csdn.net/zds13257177985/article/details/80084884