Pros pls help, Pointer to array

This is a discussion on Pros pls help, Pointer to array within the C Programming forums, part of the General Programming Boards category; Hi, im trying to find out of the content of the file is a number, not a number(operator/ char) and ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    7

    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, " ");
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int i[]= atoi( tokenPtr );
    When declaring an unsized array, you must specify all of the elements of its size at compile time. The function call to atoi doesn't happen until runtime, so this assignment is invalid. However, it should be noted that atoi is only ever going to return a single integer, so I don't see why you're even bothering using an array at all. Also:
    Code:
    if( i[a]*4 == 0 )
    Why are you bothering to multiply by anything here? Any integer that multiplied by a non-zero number nets you 0 is 0 in the first place. Thus:
    Code:
    if( i[a] == 0 )
    is all you need (were it actually what you want and not something entirely incorrect).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    7

    token into array.

    i see the problem now.
    i believe i want to put the token into the array
    but im having problems getting the token into an array
    i tried char array[] = tokenPtr;
    but returns a error, initializer must be constant...

    if i can solve that part i would be able to use a for loop to test each part of array(word) using atoi/isdigit for number, isaplha for letters, other than tt for operators.

    ive been trying for days and i can't get the token into an array. Some help pls.
    thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    sorry if i portray my qn not clearly enough...
    but i really like to thank you for your time...
    i would like to create a program which reads the content of the file and
    break it into 'words'. For each 'word' i have to find out if it is a number, not a number or a invalid string.

    for example,
    word = 1223 so it is a number.
    word = + so it is not a number.
    word = -8ha so it a invalid string.

    my program i did so far is to

    open the file and read it
    get the content of the file into a string
    tokenize the string

    so now i have each token for each word.
    i like to break each word to each letter/num/operator

    but im having problems getting the pointer of the token into a new array.

    i tried char NewArray[] = tokenPtr;

    but with errors. like initializer is not constant.

    after getting the token into a array,
    using a for loop,
    i test each element of the array for letter, num, operator.

    if it is a num return int b= 1
    if it is a letter return int c= 1
    if it is a operator return int d= 1

    the purpose of doing so is to find the invalid string

    so i use a if statement.

    if(b =1 && c =1 ) return invalid string.

    i dont know if the above algorithm works but this is what i thought up so far, as i can't get pass on how to put the pointer into a new array.

    Some1 kindly help pls.
    thanks

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >word = 1223 so it is a number.
    >word = + so it is not a number.
    >word = -8ha so it a invalid string.
    Okay, what about -123? What about +17? What about 1.0e+5? A number is a very general term, and if you want to handle all numbers then you need to consider integers, floating-point, and all of the eccentricities that go with them. If you're only dealing with positive integers, the problem is trivial:
    Code:
    int is_number ( char a[], int n )
    {
      int i;
    
      for ( i = 0; i < n; i++ ) {
        if ( !isdigit ( a[i] ) )
          break;
      }
    
      return i == n;
    }
    If you want to handle proper C constant values, the problem becomes much harder.

    >i tried char NewArray[] = tokenPtr;
    Arrays cannot be assigned. You'll need to allocate enough memory for the new array so that it can hold all of the characters in the string, then copy them one by one, preferrably with a standard library function:
    Code:
    char *newArray = malloc ( strlen ( tokenPtr ) + 1 );
    
    if ( newArray == NULL ) {
      /* Handle the error */
    }
    
    strcpy ( newArray, tokenPtr );
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    Quote 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;
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So test the characters to see if they're '-' or '+', and if they're at the beginning of the number. If you're at the beginning, meaning you haven't read any numeric portions, and you encounter a '+' or a '-', followed by numbers, it's fine. Just make something to keep track of the 'sign' being encountered, and deal with possible cases from there.

    Or just save yourself the trouble and use sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21