Problems:
-
If the user enters something that doesn’t contain a
'^', then your code exhibits Undefined Behavior as num is not initialized hereif(isdigit(num))If the input does contain a
'^', then theifwill be true. -
Here:
strcat(power,parts[k]);strcatfinds the NUL-terminator in its first argument and overwrites its second argument from that position. Butpowerisn’t initialized! So, this also invokes Undefined Behavior. Furthermore,parts[k]is achar, butstrcatexpects both its arguments to be NUL-terminated and be of typeconst char*.
Initialize `power by usingchar power[30]={0};and change the
strcattostrcat(power,&parts[k]); -
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\0terminated -
Here:
scanf("%s",parts);The user could enter some string more than 29 characters long(+1 for the
\0terminator). This would also cause Undefined Behavior. Fix it by limiting the number of characters to be scanned by usingscanf("%29s",parts);The
scanfwill scan at most 29 characters(+1 for the\0terminator) from the standard input stream when this is done.
6
solved extracting power from polynomials