?
Solved

Problem with Pointers Arrays

Posted on 2003-10-23
3
Medium Priority
?
244 Views
Last Modified: 2010-04-15
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.
            
                                                            
      
0
Comment
Question by:MastaBlasta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 750 total points
ID: 9609726
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?


 


   
 
}
0
 

Author Comment

by:MastaBlasta
ID: 9610000
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.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9612912
#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.


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question