Pls help with C code. Evaluating data types.

Thread Solved

Join Date: Aug 2005
Posts: 13
Reputation: kokopo2 is an unknown quantity at this point 
Solved Threads: 0
kokopo2 kokopo2 is offline Offline
Newbie Poster

Pls help with C code. Evaluating data types.

 
0
  #1
Aug 13th, 2005
Hi pros and experts. I am experiencing some trouble in doing up a code which could evaluate the input into the different data types.

What i managed to acheived is to read a file and its content.
I used strtok to break the string into various 'tokens'.
Now the problem is how do i differentiate the tokens into the different data types. I tried using 'isdigit' and 'atoi' to no avail. help pls.

For example contents of 'myfile.txt' as follow:

12 43 + - = / -9ya -300

the program should print the output as

12 is a number.
43 is a number.
+ is a operator.
- is a operator.
/ is a operator.
-9ya is invalid.
-300 is a number.

The code i managed to acheived so far is as follows:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main()
{
FILE * pFile;
char string [100];
char *tokenPtr;
int i;

pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {

fgets (string , 100 , pFile);
puts (string);

tokenPtr = strtok( string, " ");

while( tokenPtr != NULL ){


printf("%s\n",tokenPtr );



tokenPtr = strtok( NULL, " ");

}

fclose (pFile);
}
return 0;
}


Any help is deeply appreciated.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Pls help with C code. Evaluating data types.

 
0
  #2
Aug 13th, 2005
Well, what constitutes a number? How about an operator? Determining if the token is a valid operator is simple, just keep a list of valid operator tokens and compare the string. Numbers are harder unless you restrict yourself to the basic integers where the only valid characters are, for example, the decimal digits '0' through '9'.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pls help with C code. Evaluating data types.

 
0
  #3
Aug 13th, 2005
Last edited by Dave Sinkula; Aug 17th, 2005 at 7:24 pm. Reason: Added more cross-posts.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Pls help with C code. Evaluating data types.

 
0
  #4
Aug 15th, 2005
>the program should print the output as
12 is a number.

You don't need to convert the string 12 to the number 12 at this stage according the the directions given above. You will need to convert the string to a number if you eventually want to do calculations using the different tokens isolated.

Doing this:

char num[] = "9ya";
int x = atoi(num);

may well give you x = 9; so you will likely need to validate first (as per Naure's post) and not just call the conversion functions atoi() or strtol() or whatever.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 13
Reputation: kokopo2 is an unknown quantity at this point 
Solved Threads: 0
kokopo2 kokopo2 is offline Offline
Newbie Poster

Pls help with C code. Evaluating data types.

 
0
  #5
Aug 16th, 2005
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.

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11. FILE * mFile; /* mFile is myfile.txt file pointer */
  12. char string [100]; /* string array */
  13. const char *tokenPtr; /* tokenPtr is a pointer to a char */
  14.  
  15.  
  16. mFile = fopen ("myfile.txt" , "r"); /* open file for reading*/
  17. if (mFile == NULL) perror ("Error opening file"); /*if mFile is empty, error opening file*/
  18. else {
  19.  
  20. fgets (string , 100 , mFile);
  21. puts (string);
  22.  
  23. tokenPtr = strtok( string, " ");
  24.  
  25. while( tokenPtr != NULL ){
  26.  
  27.  
  28. printf("\n%s\n", tokenPtr );
  29.  
  30.  
  31. int i[]= atoi( tokenPtr );
  32.  
  33. for(int a=0; a<4; a++) {
  34. if( i[a]*4 == 0 )
  35. printf("Not a number\n");
  36. else{
  37. printf("Number\n",);
  38. }
  39.  
  40.  
  41. tokenPtr = strtok( NULL, " ");
  42.  
  43. }
<< moderator edit: fixed code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pls help with C code. Evaluating data types.

 
0
  #6
Aug 17th, 2005
The version I'd written after this was first (multiply-) posted pretty much followed the framework that Salem presented here. I used strtol to overcome atoi's shortcomings [edit]like zero being a valid integer as well as being an error code[/edit]. I think I chose to use a different parser that keeps the original string unmodified, but I'm away from the computer where that code is located at the moment. The operator-checking function I wrote just looped through possible strings that might match. I'd recommend using into strtol and following Salem's basic framework.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 13
Reputation: kokopo2 is an unknown quantity at this point 
Solved Threads: 0
kokopo2 kokopo2 is offline Offline
Newbie Poster

Re: Pls help with C code. Evaluating data types.

 
0
  #7
Aug 17th, 2005
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...

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10. FILE * mFile;
  11. char string[100]; /* Input line from file */
  12. char st[100]; /* Temporary storage for the line */
  13.  
  14. char nt[4];
  15. char *tokenPtr;
  16.  
  17. mFile = fopen ("myfile.txt" , "r");
  18. if (mFile == NULL) { /* If there was a problem, show the error */
  19. perror ("Error opening file");
  20. }
  21. else {
  22. fgets (string , 100 , mFile);
  23. printf("The original line: >%s<\n", string);
  24.  
  25. strcpy(st, string);
  26.  
  27. tokenPtr = strtok(st, " \n");
  28.  
  29. while( tokenPtr != NULL ){
  30. printf("%s\n", tokenPtr);
  31.  
  32. for( int x = 0; tokenPtr[ x ] != '\0'; x++ )
  33. {
  34.  
  35. /* to find invalid string, e.g. -8ha*/
  36. if( isalpha( tokenPtr[ x ] ) )
  37. int a;
  38. a = 1;
  39.  
  40.  
  41. if( isdigit( tokenPtr[ x ] ) )
  42. int b;
  43. b = 1;
  44.  
  45. if( a == 1 && b == 1)
  46. {
  47. printf("invalid\n");
  48. }
  49. }
  50.  
  51. /*to find number range from negative to positive and operator*/
  52. int i;
  53. i= atoi( tokenPtr );
  54. if( i != '\0' )
  55. printf(" Number\n");
  56. else
  57. {
  58. printf("not a number\n");
  59. }
  60.  
  61.  
  62. tokenPtr = strtok(NULL, " \n");
  63. }
  64.  
  65. }
  66. return 0;
  67. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Pls help with C code. Evaluating data types.

 
0
  #8
Aug 17th, 2005
this may work
Attached Files
File Type: txt stuffy.txt (286.7 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pls help with C code. Evaluating data types.

 
0
  #9
Aug 17th, 2005
Originally Posted by Dave Sinkula
The version I'd written after this was first (multiply-) posted pretty much followed the framework that Salem presented here. I used strtol to overcome atoi's shortcomings [edit]like zero being a valid integer as well as being an error code[/edit]. I think I chose to use a different parser that keeps the original string unmodified, but I'm away from the computer where that code is located at the moment. The operator-checking function I wrote just looped through possible strings that might match. I'd recommend using into strtol and following Salem's basic framework.
For example,
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. int num(const char *text)
  5. {
  6. char *end;
  7. int result = strtol(text, &end, 10);
  8. return end != text && *end == '\0';
  9. }
  10.  
  11. int op(const char *text)
  12. {
  13. char *oper[] = {"+","-","*","/","="};
  14. size_t i;
  15. for ( i = 0; i < sizeof oper / sizeof *oper; ++i )
  16. {
  17. if ( strcmp(text, oper[i]) == 0 )
  18. {
  19. return 1;
  20. }
  21. }
  22. return 0;
  23. }
  24.  
  25. void categorize(const char *text)
  26. {
  27. printf("%s : ", text);
  28. if ( num(text) )
  29. {
  30. puts("number");
  31. }
  32. else if ( op(text) )
  33. {
  34. puts("operator");
  35. }
  36. else
  37. {
  38. puts("invalid");
  39. }
  40. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 13
Reputation: kokopo2 is an unknown quantity at this point 
Solved Threads: 0
kokopo2 kokopo2 is offline Offline
Newbie Poster

Re: Pls help with C code. Evaluating data types.

 
0
  #10
Aug 18th, 2005
thanks for the help. I managed to solve the problem!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC