The objective is to make it so that all uppercase letters are printed out in a string, however, I must keep the bool function and use it within my code. I have resolved some issues from the past but ran up with a issue that is preventing me from fixing it. Test cases are "HEllO" -> "HEO" "my" -> "" "NAME" -> "NAME" "Is" -> "I" "AnDeRsON" -> "ADRON".
#include <iostream>
using namespace std;
bool isUpperCase(char ch){
if(ch >= 'A' and ch <= 'Z'){
return true;
}
return false;
}
int main() {
string a = "";
cin >> a;
string c = "";
for(int i = 0; i < a.length(); i++)
{
if (isUpperCase(a[i])) i++;
{
c += a[i];
}
}
cout << c << endl;
}
Now whenever I do a case like "CHADnigeria" it turns it into "HDnigeria" even though I want it to say "CHAD". It also removes the capitals which I do not want and it should be removing the lowercases. "DancingInTheSky" turns into "ancingnheky" which should be "DITS". Reminder that the bool function must not be changed though.
I updated your program so that it works:
#include <iostream>
using namespace std;
bool isUpperCase(char ch){
if(ch >= 'A' and ch <= 'Z'){
return true;
}
return false;
}
int main() {
string a = "";
cin >> a;
string c = "";
for(int i = 0; i < a.length(); i++)
{
if (isUpperCase(a[i]))
{
c += a[i];
}
}
cout << c << endl;
}
What was your mistake: You put i++
after the if
statement, which caused the weird output.
I hope I could help a little bit