Originally Posted by
quzah
I don't see why you even need to move it to another array. You've already got a string with the data in it. Just walk through it and test each piece:
Code:
for( x = 0; tokenPtr[ x ] != '\0'; x++ )
{
if( isdigit( tokenPtr[ x ] ) )
printf("this is a digit");
if( isalpha( tokenPtr[ x ] ) )
printf("this is an alphabetic character");
if( ispunct( tokenPtr[ x ] ) )
printf("this is punctuation" );
}
Or whatever it is you're trying to do...
Quzah.
Hi guys, i like to thank all who has helped me so far. I managed to come up with some sort of code, to find the invalid string. e.g -8ha .
However im having problems getting it to print whether is it a invalid string once only. Also if there is a number in the invalid string it will print the number out too.
Also, if it is a negative number, the program will print it as invalid string too.
/*the output is .
Contents of file : 12 3 + * -79 -8ha
12 number
3 number
+ not number
* not number
-79 invalid
invalid
invalid
number
-8ha invalid
invalid
number
*/
i managed to print the positive number and operator correctly,
however i just cant seem to print the negative number and invalid string correctly.
can some1 help pls?
thanks in advance...
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
FILE * mFile;
char string[100]; /* Input line from file */
char st[100]; /* Temporary storage for the line */
char nt[4];
char *tokenPtr;
mFile = fopen ("myfile.txt" , "r");
if (mFile == NULL) { /* If there was a problem, show the error */
perror ("Error opening file");
}
else {
fgets (string , 100 , mFile);
printf("The original line: >%s<\n", string);
strcpy(st, string);
tokenPtr = strtok(st, " \n");
while( tokenPtr != NULL ){
printf("%s\n", tokenPtr);
for( int x = 0; tokenPtr[ x ] != '\0'; x++ )
{
/* to find invalid string, e.g. -8ha*/
if( isalpha( tokenPtr[ x ] ) )
int a;
a = 1;
if( isdigit( tokenPtr[ x ] ) )
int b;
b = 1;
if( a == 1 && b == 1)
{
printf("invalid\n");
}
}
/*to find number range from negative to positive and operator*/
int i;
i= atoi( tokenPtr );
if( i != '\0' )
printf(" Number\n");
else
{
printf("not a number\n");
}
tokenPtr = strtok(NULL, " \n");
}
}
return 0;
}