-1
cout << "Please enter colors for the two cuboids: " ;
cin >> color1 >> color2;


if ( color1 == color2)
    cout << "Color names cannot be the same, good bye..." << endl;

Like here I need to check if color 1 is equal to color2 for example rEd and RED should be considered the same but I should not use for loops to solve this is there any other way?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70

4 Answers4

0

Here's a solution with no loops. It converts each character in the string to lower case before comparing them, and uses tail recursion to iterate over the string. I'm guessing this is the intention of the problem you've been set, though some would say that tail recursion is a loop as well.

#include <cctype>
#include <string>

bool string_eq_ci(const std::string& x, const std::string& y);
bool string_loop(const std::string& x, const std::string& y, size_t i);

    ...
    if (string_eq_ci(color1, color2)))
        cout << "Color names cannot be the same, good bye..." << endl;
    ...

bool string_eq_ci(const std::string& x, const std::string& y)
{
    return x.size() == y.size() && string_loop(x, y, 0);
}

bool string_loop(const std::string& x, const std::string& y, size_t i)
{
    return i == x.size() || (
            tolower(static_cast<unsigned char>(x[i])) == 
            tolower(static_cast<unsigned char>(y[i])) && 
        string_loop(x, y, i + 1));
}
john
  • 85,011
  • 4
  • 57
  • 81
0

You can use either of:

  1. Use strcasecmp(), for example:

    if(strcasecmp(color1.c_str(), color2.c_str()) == 0) { .. equal colors.. }

  2. Convert both colors to lowercase (or uppercase), and compare strings, as you wrote in your question.You can do it with std::toupper.

olegarch
  • 3,670
  • 1
  • 20
  • 19
0

For English colors without any iteration (loops, recursion, etc.) (beside those of IO operations):

#include <iostream>

int main()
{
    std::string color1;
    std::string color2;

    std::cout << "Please enter colors for the two cuboids: " ;
    std::cin >> color1 >> color2;
    
    if ( (color1.size() == color2.size()) 
        && ( (color1.front() | 0x20) == (color2.front() | 0x20) )
        && ( (color1.back() | 0x20) == (color2.back() | 0x20) )
       )
        std::cout << "Color names cannot be the same, good bye..." << std::endl;
}

wandbox

Exclaimer:

This is a little bit of a joke. I did not check if this code works ok for all colors and it may be wrong. But even so there may be a way to write code without any iterations.

Logman
  • 4,031
  • 1
  • 23
  • 35
0

Since your assignment requires you not to write loops, my guess is that you are expected to use algorithms instead. This is a good thing, since algorithms often convey the intent of the code better than loops can.

Here's a function copied directly from here that converts all the characters of a string to lowercase:

std::string str_tolower(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(), 
                   [](unsigned char c){ return std::tolower(c); } 
                  );
    return s;
}

Now you can use this function in the comparison of the strings like this:

if (str_tolower(color1) == str_tolower(color2))
  // ...
cigien
  • 57,834
  • 11
  • 73
  • 112