Pros pls help, Pointer to array
Hi, im trying to find out of the content of the file is a number, not a number(operator/ char) and invalid number.
number = 12, -14 etc
not a number = +, -, /, *, da, etc
invalid number = -144dad, 4i02d, etc
i managed to print the number and not a number out but i just can't figure how to get the invalid number...
the problem is as follows
read content of file,
break the content into words; using strtok, delimiter is 'white space'
now the problem im facing is how do one put the 'tokenPtr' into an array?
Im thinking of, after breaking into words, but the word into one array . However im facing a problem, initializer must be constant.
After breaking each word into 'letters,numbers or operators'.
use atoi to change each 'letters,numbers or operators' into a integer. it will return a integer if integer but return 0 if it is a letter or operator.
i add all the individual parts of the array.
if array= 0, then it is not a number.
if array is 1 or more , it is a number.
another problem im facing if the number(from content of the file) is 0, it will return not a number..
i can't seem to set the content of pointer tokenPtr into another array either.
some1 help pls?
thanks in advance.
the code i come up so far.
i assume each word has a maximum of 4 size.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
FILE * mFile; /* mFile is myfile.txt file pointer */
char string [100]; /* string array */
const char *tokenPtr; /* tokenPtr is a pointer to a char */
mFile = fopen ("myfile.txt" , "r"); /* open file for reading*/
if (mFile == NULL) perror ("Error opening file"); /*if mFile is empty, error opening file*/
else {
fgets (string , 100 , mFile);
puts (string);
tokenPtr = strtok( string, " ");
while( tokenPtr != NULL ){
printf("\n%s\n", tokenPtr );
int i[]= atoi( tokenPtr );
for(int a=0; a<4; a++) {
if( i[a]*4 == 0 )
printf("Not a number\n");
else{
printf("Number\n",);
}
tokenPtr = strtok( NULL, " ");
}