2

This was a challenge off of Codewars. The function takes a string as an argument and it returns a string that is in the fraction format. If the input characters are between 'a' and 'm' it is supposed to be an 'error free' status, otherwise it is an error message.

example: aaab should return 0/4 where the numerator is the amount of error codes and the denominator is the string length minus the leading NULL character. xxa would return 2/3.

The question I have is how do I put an integer into a char array? As you will see I am adding a '0' to the integer which works for numbers 0-9 but for bigger numbers it prints other characters.

I'm using Xcode as my IDE.

Here's the code:

#include <stdio.h>
#include <string.h>

char* printerError(char *s)
{
    int i, string_size, error_total;
    unsigned long int size = strlen(s);

    char status[3];
    char *ptr;

    string_size = error_total = 0;
    for (i=0; i<size; i++)
    {
        if (s[i] != '\0' && s[i] != EOF)
        {
            string_size++;
            if (s[i] > 'm')
                error_total++;
        }
    }

    status[0] = error_total + '0';
    status[1] = '/';
    status[2] = string_size + '0';

    ptr = status;
    printf("%s\n", status);
    return ptr;
}

int main(void) {
    printf("error code: %s\n", printerError("aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz"));
    return 0;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
RobotMan
  • 605
  • 1
  • 5
  • 10
  • 1
    `printf("%d/%d\n",error_total,string_size);` will print it on standard output, then use `sprintf` to print it into a string. Beware that the string length is not constant. – Jean-Baptiste Yunès May 04 '19 at 14:33
  • Note: although the title of the dupe target is opposite to the title of this question, the actual question posed here is *also* opposite to the title: "The question I have is how do I put an integer into a char array?" – John Bollinger May 04 '19 at 14:39
  • `char status[3]` is not large enough for the *string* `"2/3"`. You have more problems and more serious ones too!! – pmg May 04 '19 at 14:39
  • 1
    @Jean-BaptisteYunès Use `snprintf()` to protect from buffer overflowing. – alx - recommends codidact May 04 '19 at 14:39
  • grrr question closed to early. out of the previous remarks `if (s[i] != '\0' && s[i] != EOF)` can be removed, you cannot reach the null char because you iterate under the index return by _strlen_ and EOF is generaly not a _char_ but an _int_ – bruno May 04 '19 at 14:40
  • @bruno thanks for pointing that out. – RobotMan May 04 '19 at 14:42
  • and what do you expect from `string_size + '0'` ? as said do `sprintf(status, "%d/%d\n",error_total,string_size);` with a larger status. Also you return a pointer to a local var, the behavior is undefined – bruno May 04 '19 at 14:43
  • @JohnBollinger This isn't a duplicate. The supposed "duplicate" is asking how to convert an int into a string. This is asking how to convert a string into an int. – S.S. Anne May 04 '19 at 14:44
  • I would suggest `atoi()`, until this gets reopened, at which point I can answer more thoroughly. I've raised a moderator flag, so hopefully someone might notice this. – S.S. Anne May 04 '19 at 14:46
  • 1
    @JL2210 _atoi_ for what ? The goal is to produce a string from an int, not the reverse. And _atoi_ must **never** be used because does not detect error – bruno May 04 '19 at 14:47
  • @JL2210 I read about using atoi() but saw that it was not in the standard library and wasn't best practice or something along those lines idk. – RobotMan May 04 '19 at 14:50
  • @JL2210, as I already noted in a previous comment, this question is *not* asking to convert a string to an int, the title notwithstanding. It is asking the reverse, which is exactly what is addressed by the dupe target. Again, the OP's "how do I put an integer into a char array?" sums it up nicely. – John Bollinger May 04 '19 at 14:51
  • `atoi()` made it into the C11 standard. – S.S. Anne May 04 '19 at 15:26

0 Answers0