How to write a part of the program that will find two words maximum in length, and print vowels (without repetitions) that have been encountered in them. Characters can only be uppercase Latin letters.

Here that already is:

#include "stdafx.h" #include "iostream" #include "conio.h" #include <set> #include <string> using namespace std; int main(array < System::String ^ >^args) { set < char >simbols; set < char >digits; char *s1 = "QWERTYUIOPASDFGHJKLZXCVBNM"; for (int i = 0; i < strlen(s1); i++) { simbols.insert(s1[i]); } int n = 0; do { cout << "Vvedite kolichestvo slov (ne bolee 10): "; cin >> n; if ((n <= 0) || (n > 10)) // количество слов cout << "Oshibka vvoda" << endl; } while ((n <= 0) || (n > 10)); char strs[10][80]; for (int i = 0; i < n; i++) { cout << "Vvedite slovo: "; cin >> strs[i]; cout << strs[i] << " slovo " << endl; int tmp = 0; for (int j = 0; j < strlen(strs[i]); j++) { if (simbols.count(strs[i][j]) == 1) { //гласные EYUIOA } else { cout << "Vstretilsa nedopustimii simvol" << endl; i--; break; } } } cout << " Vivod bykv = "; _getch(); return 0; } 
  • @mishel, here’s inside the brackets where you don’t have a code and you need to find the 2 longest words (these are strlen (strs [i]) are the biggest) and remember their indices (i in this place) in variables, say, m1 and m2. Then write a function that displays vowels without repeating from a word and call it once with strs [m1], and call it once with strs [m2]. And the problem is solved. - avp
  • If it is easy for you, you can write this, otherwise your head is spinning = ( - mishel
  • This is what I have for finding the two maximum elements, but it gives out the results as something strange, or simply wrote incorrectly int m1 = 0, m2 = 0; for (int i = 0; i <n; i ++) {if (strlen (strs [i])> m1) {m1 = strlen (strs [i]); } else if (strlen (strs [i])! = m1) {if (strlen (strs [i])> m2) {m2 = strlen (strs [i]); }}} cout << "VIVOD SLOV" << m1 << '' << m2 << endl; - mishel
  • @mishel, study the answer that @alexlz wrote to you. And then apply the same algorithm not to string (this is for @alexlz) but to your char []. - avp
  • Cheto, I still do not understand = / - mishel

1 answer 1

Without checks, assuming that all input is correct:

 #include <iostream> #include <set> #include <string> using namespace std; int main() { string s[2] = {"", ""}; char v[] = {'A', 'E', 'I', 'O', 'U', 'Y'}; set<char> vowels (v, v+sizeof v); while(!cin.eof()) { string w; cin >> w; if(w.length() > s[0].length()) { s[1] = s[0]; s[0] = w; } else if (w.length() > s[1].length()) { s[1] = w; } } for(int i=0; i<2; i++) { cout << "Слово " << i << "(" << s[i] << ")> "; set<char> v1; for(string::iterator it = s[i].begin(); it < s[i].end(); it++) if(vowels.find(*it) != vowels.end()) v1.insert(*it); for(set<char>::iterator it = v1.begin(); it != v1.end(); it++) cout << *it << ' '; cout << endl; } return 0; } 
  • @alexlz, here I am always interested in what purpose instead of string ts = "abc"; for (int i = 0; i <ts.length (); i ++) cout << ts [i] << '\ n'; write (well, std:: doesn’t apply to your example, this is for the sake of effect) std :: string ts ("abc"); for (std :: string :: iterator it = ts.begin (); it <ts.end (); it ++) std :: cout << * it << '\ n'; IMHO is just their "scholarship" want to show. - avp
  • Alas, with knowledge of C ++, I am far from all right, so I cannot explain. But the language itself is such a terrible mess of C, OOP and much more, which is simply surprising that the programs still work on it. And, since this design works for others, I used it. (I'm just afraid of this language). Especially string'i, which the hell knows what it consists of: char, wchar, characters of variable length, etc. Of course, operation [] should work fine, but what changing living conditions should produce .length () is another question. - alexlz
  • @alexlz, golden words (I'm not kidding). So I have the impression (especially when I see how beginners suffer, trying to write down some simple algorithm) that C ++ is not the best language for learning, and if frankly, for practical programming. - .length () should return the current length. In practice, knowing that the length of the string does not change within the meaning of the problem, I will write for (int i = 0, l = ts.length (); i <l; i ++) {...} Actually, even if the length changes, it’s better to write so, obviously changing l in the loop, especially if the length changes in the functions called in for - avp
  • Well, in general, the interpretation [ length ] [1] is ambiguous. Returns to the string. What is the number of characters? Anyone? Single byte? Double bytes? Or MBCS? It would be better to write - " length returns the number of single-byte char elements that make up the string " [1]: cplusplus.com/reference/string/string/length - gecube