String
Jun 6, 2017 at 10:13am UTC
no comment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
vector<string> tachtu(string sent){
vector<string> words;
int len = sent.length();
int i = 0, start = 0;
for (; i<len; i++){
if (sent.at(i)==' ' ){
words.push_back(sent.substr(start, i-start));
start=i+1;
}
}
words.push_back(sent.substr(start, i-start));
return words;
}
void maxmin(const vector<string>& words){
int len = words.size();
int maxlen = 0, minlen = 0, imax, imin;
string strmax="" , strmin="" ;
if (len > 0){
int i=0;
strmax=strmin=words[i];
imax=imin=0;
maxlen=minlen=words[i].length();
for (i=1; i<len; i++){
if ((words[i].length() > maxlen)||(words[i].length()==maxlen)&&(words[i].compare(strmax)<0)){
imax = i;
strmax = words[imax];
maxlen = words[i].length();
}
if ((words[i].length() < minlen)||(words[i].length()==minlen)&&(words[i].compare(strmin)<0)){
imin = i;
strmin = words[imin];
minlen = words[i].length();
}
}
}
cout << "Max: " << strmax << ": " << maxlen << endl;
cout << "Min: " << strmin << ": " << minlen << endl;
}
int main(){
string sent;
cout << "Nhap vao mot xau: " ;
getline(cin, sent);
vector<string> words = tachtu(sent);
maxmin(words);
return 0;
}
Jun 6, 2017 at 10:15am UTC
That's.... nice?
Was there a point to this? Do you have a problem? A question?
Jun 6, 2017 at 6:45pm UTC
Standard library is there to be taken advantage of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <iterator>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
std::vector<std::string> tachtu(const std::string& sent);
void maxmin(const std::vector<std::string>& words);
void waitForEnter();
int main()
{
std::cout << "Nhap vao mot xau: " ;
std::string sent;
std::getline(std::cin, sent);
std::vector<std::string> words = tachtu(sent);
maxmin(words);
waitForEnter();
return 0;
}
std::vector<std::string> tachtu(const std::string& sent)
{
std::istringstream sstr(sent);
std::vector<std::string> words {std::istream_iterator<std::string>{sstr},
std::istream_iterator<std::string>{}};
return words;
}
void maxmin(const std::vector<std::string>& words)
{
std::string longest, shortest(100, ' ' ); // reasonable length
for (const auto & str : words) {
if (str.length() > longest.length()) { longest = str; }
if (str.length() < shortest.length()) { shortest = str; }
}
std::cout << "Longest: \"" << longest << "\": "
<< longest.length() << '\n' ;
std::cout << "Shortest: \"" << shortest << "\": "
<< shortest.length() << '\n' ;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
Nhap vao mot xau: As long as poverty, injustice and gross inequality persist in our world,
none of us can truly rest (Nelson Mandela)
Longest: "inequality": 10
Shortest: "As": 2
Press ENTER to continue...
Topic archived. No new replies allowed.