amazing56789
amazing56789

Reputation: 16

Inline sprintf() for defining string as num in C?

So a bit ago I was warming up and doing some very simple challenges. I came across one on edabit where you need to make a function to add the digits of a number and tell if the resulting number is "Oddish" or "Evenish" (ie oddishOrEvenish(12) -> "Oddish" because 1 + 2 = 3 and 3 is odd) so I solved it with some simple code

# include <stdio.h>
# include <stdlib.h>

char* odOrEv(int num);
int main(int argc, char* argv[]) {
    printf("%s", odOrEv(12));
}

char* odOrEv(int num) {
    char* strnum = (char*) malloc(11);
    char* tempchar = (char*) malloc(2); // ik i can declare on one line but this is neater
    int total = 0;
    sprintf(strnum, "%d", num);
    for (int i = 0; i < sizeof strnum; i++) {
        tempchar[0] = strnum[i];
        total += (int) strtol(tempchar, (char**) NULL, 10);
    }
    if (total % 2 == 0) return "Evenish";
    return "Oddish";
}

and it worked first try! Pretty rudimentary but I did it. i then thought hey this is fun howabout I make it better, so I got it down to

# include "includes.h"

char* odOrEv(int num);
int main(int argc, char* argv[]) {
    printf("%s", odOrEv(13));
}

char* odOrEv(int num) {
    char* strnum = (char*) malloc(11);
    int total = 0;
    sprintf(strnum, "%d", num);
    while (*strnum) total += (int) *strnum++;
    return total % 2 == 0 ? "Evenish" : "Oddish";
}

just 5 lines for the function. Since I'm so pedantic though, I hate that I have to define strnum on a different line than declaring it since I use sprintf. I've tried searching, but I couldn't find any functions to convert int to string that I could use while declaring the string (e.x. char* strnum = int2str(num);). So is there any way to cut off that one line?

srry if this was too big just tried to explain everything P.S. don't tell to use atoi() or stoi or any of those since they bad (big reason long to eplain) also I'd prefer if I didn't have to include any more directories but it's fine if I do

EDIT: forgot quote added it

Upvotes: 0

Views: 160

Answers (2)

Barmar
Barmar

Reputation: 781706

You don't actually have to add the digits. The sum of even digits is always even, so you can ignore them. The sum of an odd number of odd digits is odd, the sum of an even number of odd digits is even. So just loop through the digits, alternating between oddish and evenish every time you see an odd digit.

You can loop through the digits by dividing the number by 10 and then checking whether the number is odd or even.

char *OddorEven(int num) {
    int isOdd = 0;
    while (num != 0) {
        if (num % 2 != 0) {
            isOdd = !isOdd;
        }
        num /= 10;
    }
    return isOdd ? "Oddish" : "Evenish";
}

Upvotes: 1

0___________
0___________

Reputation: 67713

To be honest it the one of the weirdest functions I have ever seen in my life.

You do not need strings, dynamic allocations and monster functions like sprintf or strtol.

char* odOrEv(int num) 
{
    int sum = 0;
    while(num)
    {
        sum += num % 10;
        num /= 10;
    }
    return sum % 2 == 0 ? "Evenish" : "Oddish";
}

Upvotes: 2

Related Questions