Mark
Mark

Reputation: 225

How to modify a string and return it?

I'm trying to modify a string and then return it in C. But the code doesn't work well. When I run the function in main, the wordOfNum returns an empty string

Here's the code:

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

char *wordOfNum(int number, int spec) {

    char *word = malloc(strlen("Example") + 1);

    if (spec == 0) {
        switch (number) {
        case 1:
            strcpy(word, "one");
            break;
        case 2:
            strcpy(word, "two");
            break;
        case 3:
            strcpy(word, "three");
            break;
        case 4:
            strcpy(word, "four");
            break;
        case 5:
            strcpy(word, "five");
            break;
        case 6:
            strcpy(word, "six");
            break;
        case 7:
            strcpy(word, "seven");
            break;
        case 8:
            strcpy(word, "eight");
            break;
        case 9:
            strcpy(word, "nine");
            break;
        }
    }
    else if (spec == 1) {
        switch (number) {
        // case 1: return "one";
        case 2:
            strcpy(word, "twenty");
            break;
        case 3:
            strcpy(word, "thirty");
            break;
        case 4:
            strcpy(word, "fourty");
            break;
        case 5:
            strcpy(word, "fifty");
            break;
        case 6:
            strcpy(word, "sixty");
            break;
        case 7:
            strcpy(word, "seventy");
            break;
        case 8:
            strcpy(word, "eightty");
            break;
        case 9:
            strcpy(word, "ninety");
            break;
        }
    }
    return word;
}

void exercise1() {

    char num[4];
    printf("This is exercise 1: \n");
    printf("Enter a 4 digit number: ");
    scanf("%[^\n]%*c", num);
    printf("%s thoundsand %s hundred %s %s", wordOfNum(num[0], 0), wordOfNum(num[1], 0), wordOfNum(num[2], 1), wordOfNum(num[3], 0));
}

int main() {

    exercise1();
    return 0;
}

I think the problem is about scoping. At wordOfNum I'm using a switch nested in an if so the word doesn't change as supposed. When I strcpy word 1 level up, which means only in a single if, the word can change

Upvotes: 2

Views: 124

Answers (1)

anastaciu
anastaciu

Reputation: 23832

So the problem is you are mixing ints and chars, what you pass as a parameter is actually a char, a simple way to fix this is to convert the char you pass as number argument to int. If you want to avoid this conversion you can change your switches from case 1:... to case '1':..., all of them, of course.

You should also use char num[5]; space for a null terminator, and scanf("%4[^\n]%*c", num); to avoid buffer overflow.

There is still the matter of verifying if the inputted value is really a 4 digit string otherwise the results will not be the expected.

Demo

char *wordOfNum(int number, int spec) { //or char number

    //...

    int num = number - '0'; //convert char to int

    if(num > 9 || num < 0){ //check if input is a 4 digit number
        puts("The input must be a 4 digit number!");
        exit(EXIT_FAILURE); // or handle it as you see fit
    }

    if (spec == 0) {
        switch (num)
        {
            //...
        }
    }
    else if (spec == 1) {
        switch (num)
        {
            //...
        }
    }
    return word;
}

As mentioned by @DeiDei in the comment section, you could avoid malloc by simply using word = "one"; etc. instead of strcpy(word,"one"); and change the return type to const char* to avoid attemps to edit the string literals.

Upvotes: 3

Related Questions