Read in a whole line and a word at a time in c++, compare the size and length of two strings

 
 
The following code implements reading a whole line from standard input and reading a word
    comparison of two strings

 
 
#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);

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

void readOneLine() //P81->3.2 Read a whole line from standard input at a time
{
	string line;
	while (getline(cin, line))
		cout << line << endl;
}
void readOneWord() //P81->3.2 Read one word at a time from standard input
{
	string word;
	while (cin >> word)
		cout << word << endl;
}

void bigString(string s1, string s2) //P81->3.4 Compare the size of two characters
{
	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 Compare the length of two characters
{
	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;
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846343&siteId=291194637