There is a task, a line is found, when outputting there should be two lines, one displays the characters only in upper case, the second only in the lower case. He began to write, a cycle to determine the lower case, displays everything, but stops if there is a character with an upper case on his way. How to correct the code?

#include "stdafx.h" #include <iostream> #include <cstdio> #include <cctype> using namespace std; int main() { setlocale(LC_ALL, "Russian"); int str0=0; char str[100],str1, str2; cout << "Строка: "; cin >> str; while (str[str0]) { str1 = str[str0]; if (islower(str1)) { cout << str1; str0++; } } system("pause"); return 0; } 
  • Your program should work only for English? Or only for English and Russian? - Unick
  • @Unick there was no indication of this, so for now I’m only doing English - Mr.Flatman
  • Well, you have already been answered, but I’ll pass on your style of naming variables - you shouldn’t do that, str is an array, str0 is an integer index, and str1 is a symbol in general. Themselves confused, not to mention others. You're not a partisan writing programs for the Gestapo? And also - well, what's the point of str1 variable? - Harry
  • @Harry creating them, I expected that the variable str1 would be responsible for outputting only the lower case, and str2, respectively, for outputting the upper register, well, I’m stuck with this, I don’t catch up with how to continue to finish the cycle for the upper case. - Mr.Flatman

1 answer 1

str0 ++; it is necessary to take out from if.

Like this:

 while (str[str0]) { str1 = str[str0]; if(islower(str1)) { cout << str1; } str0++; } 

Otherwise, when you encounter a character in upper case, your index increment stops and you are stuck in this character forever.