Home > OS >  How do I make a program that replaces int to char (without using while/for) in C?
How do I make a program that replaces int to char (without using while/for) in C?

Time:12-17

I'm new to programming and I'm struggling with a challenge. What I'm trying to do is a program that reads whole numbers and returns it replacing the even numbers with a "." and odd numbers with a "-" (starting with unity, then ten, then hundred and thousand. So a number like 8878 is being read backwards: 8788).

For example:

Input:

8878
2122
47

Output:

.-..
..-.
-.

My two questions are as follows:

How can I make this code convert only the amount of digits. For example "47" in my program is returning "-..." instead of "-." which is my goal.

How can I make this code always ask for the next input until it completes 10 inputs (and without using while/for)?

#include <stdio.h>

int main() {
int number;

scanf("%d", &number);

int unit = number % 10;
int ten = number / 10 % 10;
int hundred = number / 100 % 10;
int thousand = number / 1000 % 10;

char even = '.';
char odd = '-';

// unit

if (unit % 2 == 0) {
    printf("%c", even);
} else if (unit % 2 != 0) {
    printf("%c", odd);
} else {
    printf("");
}

// ten

if (ten % 2 == 0) {
    printf("%c", even);
} else if (ten % 2 != 0) {
    printf("%c", odd);
} else {
    printf("");
}

// hundred

if (hundred % 2 == 0) {
    printf("%c", even);
} else if (hundred % 2 != 0) {
    printf("%c", odd);
} else {
    printf("");
}

// thousand

if (thousand % 2 == 0) {
    printf("%c", even);
} else if (thousand % 2 != 0) {
    printf("%c", odd);
} else {
    printf("");
}

return 0;
}

CodePudding user response:

Before writing code, it may help to precisely analyze the requirements. Here we have:

  • no loops: Ok it is a hint that recursion is to be used
  • process digits in opposite numbers: ok, we can extract the digits in opposite number by using the number modulo 10 and then the number divided by 10
  • display . for an even digit and ' for an odd digit: ok the last digit is even if and only if the number is - yep no need for modulo...
  • we will display one processed number per line: ok write a new line after each number
  • only process positive numbers: ok we will use an unsigned int type

Corner case

A 0 number should be displayed as ., while it will be the sentinel value in our recursion: split the processing into an external function that will test the 0 value and a recursive internal function that will process the digits.

Now it becomes trivial to write the C code:

#include <stdio.h>

// recursively discrimates even/odd digits
void do_describe(unsigned int n) {
    if (n == 0) return; // the sentinel aka the stop condition
    putchar(n % 2 ? '-' : '.');
    do_describe(n / 10);
}

// processes one number and displays it on its own line
void describe(unsigned int n) {
    // first the 0 corner case
    if (n == 0) {
        putchar('.');
    }
    else {
        do_describe(n);
    }
    putchar('\n');
}

int main() {
    // external loop: read integers one at a time
    for (;;) {
        unsigned int n;
        // stop when not a positive integer or on end of file
        if (scanf("%u", &n) != 1) break;
        describe(n);
    }
    return 0;
}

In above code, the main still contains a loop because it is more idiomatic in C and more robust/efficient than recursion. But it can easily be converted into a recursive function:

int recursive_loop(unsigned int max) {
    unsigned int n;
    if (max == 0) return 0;   // again the stop condition for recursion
    if (1 != scanf("%u", &n)) return 0;
    describe(n);
    return recursive_loop(max - 1);
}

int main() {
    // external recursive loop: read at most 10 integers
    recursive_loop(10);
    return 0;
}
  • Related