Solved

Problem with Pointers Arrays

Posted on 2003-10-23
3
231 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
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 250 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now