Home > Software design >  I need to write a program to get a string of numbers and check the lowest possible base that number
I need to write a program to get a string of numbers and check the lowest possible base that number

Time:11-29

//take in a number and predict the possible base of the number
#include <stdio.h>
#include <ctype.h>
#include <string.h>


int Type(char input[20]) {
    int base = 0;
    //turn all the characters in the string to upper case characters
    for(int i = 0; i = strlen(input); i  ){
        if(isalpha(input[i])){
            input[i] = toupper(input[i]);
        }
    }
    printf("%s", input);

    for(int i = 0; i = strlen(input); i  ) {
        //if characters are present in the string checkk if they are A-F
        if(isalpha(input[i])){
            if((int)input[i] >= 65 && (int)input[i] <= 70 && base < 16){
                base = 16;
            }
        }//if everything is a digit and they are below 9
        else if(isdigit(input[i]) && (int)input[i] <= 9 && base < 10){
            base = 10;
        }//if everything is below 7
        else if(isdigit(input[i]) && (int)input[i] <= 7 && base < 8){
            base = 8;
        }
        else if(isdigit(input[i]) && (int)input[i] <= 3 && base < 4){
            base = 4;
        }
        else if(isdigit(input[i]) && (int)input[i] <= 1 && base < 2){
            base = 2;
        }
        else{
            base = 0;
        }
    }
    return base;
}

int main(){
    char user_input[20];
    printf("Enter the number : ");
    scanf("c", &user_input);
    printf("%s", user_input);
    
    switch(Type(user_input)){
        case 16 : printf("The maximum possible base is %d.\n", Type(user_input));
        break;
        case 10 : printf("The maximum possible base is %d.\n", Type(user_input));
        break;
        case 8 : printf("The maximum possible base is %d.\n", Type(user_input));
        break;
        case 4 : printf("The maximum possible base is %d.\n", Type(user_input));
        break;
        case 2 : printf("The maximum possible base is %d.\n", Type(user_input));
        break;
        default : printf("Invalid number type.\n");
    }
}

so here's my code. Any idea what I'm doing wrong? The "Type" function does not execute. The compiler does not give errors so I assume the problem is with logics. is it a good idea to have a seperate function to uppercase the string? Is this a good way to check the minimum base or is there another way to do this?

Expected inputs and outputs input - E45G, output - invalid number type input - 4987, output - base is 10 input - ab10, output - base is 16

CodePudding user response:

Any idea what I'm doing wrong?

Wrong input

Rather than read a character, read a group of them.

// scanf("c", &user_input);
scanf("s", user_input); 

Walk the string looking for the max converted character

Let strtol() convert the characters, one by one, to a value.

// Return max possible base
// Return -1 on error
int MaxBase(const char *input) {
  int max_base = -1;
  // While there are characters left ...
  while (*input) {
    char s[2] = { *input, '\0' }; // Make a short string
    char endptr;
    int value = (int) strtol(s, &endptr, 36); // Attempt base 36
    // If no conversion ...
    if (s == endptr) {
      return -1; // Some non-numeric character.
    }
    if (value >= max_base) {
      max_base = value   1;
    }
    input  ; // Try next character.
  }
  return max_base;
}

Using strtol() avoids any case concerns and does not depend on ASCII unlike input[i] >= 65.

Interestingly MaxBase("0") would return 1 and MaxBase("Z"), MaxBase("z") would return 36.

MaxBase("") and MaxBase("98AZaz!") return -1.

More code needed to handle sign characters.

  • Related