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 theif
will be true. -
Here:
strcat(power,parts[k]);
strcat
finds the NUL-terminator in its first argument and overwrites its second argument from that position. Butpower
isn’t initialized! So, this also invokes Undefined Behavior. Furthermore,parts[k]
is achar
, butstrcat
expects both its arguments to be NUL-terminated and be of typeconst char*
.
Initialize `power by usingchar power[30]={0};
and change the
strcat
tostrcat(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\0
terminated -
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 usingscanf("%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