• C

Problem with Pointers Arrays

Hi all,

I have one pointer and one pointer array, both of char type. I have read all elements of a file into the pointer, character by character. The elements of the file contain spaces, dots, commas etc. I want to filter out the punctuations and transfer ONLY the words into the pointer array.

This is the first part of my code:-

char *ptr;
FILE *ff=fopen("test.txt","r");

ptr=(char *)malloc(1000);
fscanf(ff,"%[a-z A-Z0-9.',!-]\n",ptr);

This correctly assigns all elements of the file(which contains a paragraph with word,sentences,puncs etc). Now I have this pointer array:-

char *string[1000];

for(i=0;i<1000;i++)
    {
         string[i]=(char *)malloc(30);
    }

I have assigned each element of the pointer array with a memory space of 30 bytes.

Now, I have an if loop which should filter out the punctuations from the ptr pointer and put only the words into the string array pointer..and each word should be stored at a diff location i.e
string[0] should be "The", string[1] should be "highlands" etc.
 
This is my loop for trying to incorporate the above functionality:-

k=0;
for(j=0;j<1000;j++)
{
               if((ptr[j])=='  ' || ptr[j]!='.' || ptr [j]!='!' || ptr[j]!=',')
      {                  
                                     k++;
                 j++;
                              
      }
            
      else
      {      
                string[k]= &(ptr[j]);// I have tried it with and without the address '&' operator but to no avail
                }
}
            

The above code assigns the entire para to the pointer array, without ignoring the spaces, commas or full stops. When I print the first element string[0]..it prints the entire paragraph. string[5] also prints the entire thing but starting from the fifth character. Whats gone wrong? Help please.

Thanks.
            
                                                            
      
MastaBlastaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrewjbCommented:
Strings have to be null terminated.

So, if you don't mind destroying the original string, you can convert the spaces etc to NULL, and assign the position of the _start_ of each word to your array.

So, something like (using sensible variable names!)


char *ptr;
char *string[1000];


int lStringIndex= 0;
int lPos;
bool lStartOfWord = true;
char c;

for ( lPos = 0 ; lPos < 1000 ; lPos++ )
{
  c = ptr[lPos];

  if ( ( c == ' ' )  || ( c== '!' ) ) // ec
  {
    // punctuation - ignore
    ptr[lPos] = 0;
    lStartOfWord = true;
   }
   else
   if ( lStartOfWord )
   {
      string[lStringIndex++] = &ptr[lPos];
      lStartOfWord = false;
    }
  }

though, of course, you've got fixed lengths and limits, which is nasty. What if the file is >1000 characters or > 30 words?


 


   
 
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MastaBlastaAuthor Commented:
Thanks a lot Andrew. Works perfectly now.


>>though, of course, you've got fixed lengths and limits, which is nasty. What if the file is >>000 characters or > 30 words?

What would you recommend then? I am using #define's for the limits.
andrewjbCommented:
#define is certainly always better than hard-coding the numbers.

Two optiosn:

1) Dynamically allocate the arrays and stuff as you need them, so you can cope with 'any' size of file.

That's more code. Perhaps you don't need to

2) Check that you've not exceeded the limits - probably warn the user if you do, or something.

Definitely do this. Can't remember the details, but you should tell fscanf to read at most 1000 characters. Also, make sure lStringIndex <= 30, otherwise abort and tell the user.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.