[Solved] extracting power from polynomials


Problems:

  1. If the user enters something that doesn’t contain a '^', then your code exhibits Undefined Behavior as num is not initialized here

    if(isdigit(num))
    

    If the input does contain a '^', then the if will be true.

  2. Here:

    strcat(power,parts[k]);
    

    strcat finds the NUL-terminator in its first argument and overwrites its second argument from that position. But power isn’t initialized! So, this also invokes Undefined Behavior. Furthermore, parts[k] is a char, but strcat expects both its arguments to be NUL-terminated and be of type const char*.
    Initialize `power by using

    char power[30]={0};
    

    and change the strcat to

    strcat(power,&parts[k]);
    
  3. The same problem is seen here too:

    strcpy(power,'1');
    

    Change it to

    strcpy(power,"1");
    

    to fix it. The double quotes make the string literal of type const char* which is \0 terminated

  4. Here:

    scanf("%s",parts);
    

    The user could enter some string more than 29 characters long(+1 for the \0 terminator). This would also cause Undefined Behavior. Fix it by limiting the number of characters to be scanned by using

    scanf("%29s",parts);
    

    The scanf will scan at most 29 characters(+1 for the \0 terminator) from the standard input stream when this is done.

6

solved extracting power from polynomials