Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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.
            
                                                            
      
0
MastaBlasta
Asked:
MastaBlasta
  • 2
1 Solution
 
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?


 


   
 
}
0
 
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.
0
 
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.


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now