Solved

Problem with Pointers Arrays

Posted on 2003-10-23
3
232 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 159
Convert image to byte array 8 189
Finding a good hash function 4 120
How to access ANSI/IEEE Std 754 or equivalent information ? 3 45
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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

920 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

17 Experts available now in Live!

Get 1:1 Help Now