0

// I tried this code

 #include<iostream>
 using namespace std;

// Function to convert characters // of a string to opposite case

#define case_change(str)\
{\
    int ln = str.length();\

// Conversion according to ASCII values

 #define for (int i=0; i<ln; i++)\
            {\
                #ifdef (str[i]>='a' && str[i]<='z'){\
                    str[i] = str[i] - 32;\
                #endif}
                //Convert lowercase to uppercase

                #elifdef (str[i]>='A' && str[i]<='Z')\{\
                    str[i] = str[i] + 32;\
                #endif}\
                //Convert uppercase to lowercase


            #endif}
    }

// Driver function

int main()
{
    string str = "GeEkSfOrGeEkS";

    // Calling the Function
    case_change(str);

    cout << str;
    return 0;
}
  • 3
    Don't wite a macro. Write a function instead. – eerorika Jun 20 '20 at 05:41
  • Welcome to stack overflow. You might want to have a look at the [help section](https://stackoverflow.com/help/how-to-ask) to learn more about writing a good question. In particular, we need a description of what went wrong when you tried to run the code. – Korosia Jun 20 '20 at 05:43
  • 1
    There's no such thing as a macro function. There are macros and there are functions. 99.999% of the time you should prefer a function. – john Jun 20 '20 at 05:44
  • I wouldn't attempt to use a macro at all for this, in C or C++. However, in C++, if you're stubborn enough to insist on using a macro, and assuming you're able to use standard header `` then `#define case_change(str) std::transform(std::begin(str), std::end(str), [](int x) -> int {return std::tolower(x) ? std::toupper(x) : (std::toupper(x) ? std::tolower(x) : x);})` will do it. Note this doesn't rely on characters using an ASCII representation. It will also work on any collection of `char` for which the compiler can detect start and end, not just strings. – Peter Jun 20 '20 at 06:26
  • @john -- the standard defines "function-like macros"; they are often referred to informally as "macro functions". – Pete Becker Jun 20 '20 at 12:32
  • @Peter -- that macro isn't quite right; the tests should be `std::islower(x)` and `std::isupper(x)`, not `std::tolower(x)` and `std::toupper(x)`. Don't you hate it that comments can't be edited? – Pete Becker Jun 20 '20 at 12:35
  • @PeteBecker - Yep, you're right. I typed too fast – Peter Jun 20 '20 at 12:36

3 Answers3

2

You appear to have got if and ifdef confused. ifdef is used to test whether a macro has previously been defined, and enable functionality based on that definition. See this question: The role of #ifdef and #ifndef

Instead, you are trying to execute a particular piece of code based on a runtime test. For that, you want if instead.

As mentioned in comments, it generally considered bad practice to write a macro when you should be writing a function. Macro vs Function in C

Korosia
  • 593
  • 1
  • 5
  • 19
1
#ifdef (str[i]>='a' && str[i]<='z')

should be

if (str[i]>='a' && str[i]<='z')

#ifdef doesn't make sense becausestr[i] must be evaluated at run time and the macro preprocessor only works at compile time.

Also #elifdef is not a legal token. For similar reason to above this should be else if.

john
  • 85,011
  • 4
  • 57
  • 81
0

You can use this code. I think it will help you

#include <iostream>
#include <bits/stdc++.h>
    
using namespace std;
    
#define CASE_CHANGE(str) ({        \
    int i = 0;                     \
    string op = "";                \
    while (str[i] != '\0') {       \
        if (isupper(str[i])) {     \
            op += tolower(str[i]); \
        }                          \
        else {                     \
            op += toupper(str[i]); \
        }                          \
        ++i;                       \
    }                              \
    op;                            \
    })

int main() {
    cout << CASE_CHANGE("babu");
}
  • This does not work for `std::string` since the case when `str[i]='\0'` is an illegal access (i.e., we would have that `i == str.size()`. Could you provide OP with an example that does not assume that `str` is `char *` (or `const char *`)? Also, indenting the macro properly would be a plus! – Lluís Alemany-Puig May 04 '22 at 13:56