Connecting Tech Pros Worldwide Forums | Help | Site Map

would i have to copy pointers? (urgent?)

Newbie
 
Join Date: Sep 2006
Posts: 7
#1: Sep 27 '06
Hi,
I have a project where i must use pointers and write a function that strips all the white spacebar spaces out of an array. I also have to count how many white spaces i have. Well i figured out how to count all the white spaces i just don't know how to strip all the white spaces out of the sentence entered. I asked the proffessor and he said one way of doing it would be to crreate another array and copy it to the new array and when it hits a white space not to copy it in to the new array. I understand that,but i don't know how to use pointers to do it.so far i
have this

void stripWhite(char *str)
{
char str2[100];

char*p;
char *t;

}

any help would be great i need it asap.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#2: Sep 27 '06

re: would i have to copy pointers? (urgent?)


You need to pointers,

1 to point at the current character to read from the source string.

1 to point at the current character to write to in the destination string.

Initally both pointers will point to the start of their respective arrays

then

Expand|Select|Wrap|Line Numbers
  1. FOREACH Character in the Source String
  2.     IF it is not a SPACE
  3.         Copy it to the destination string
  4.         Increment the destination string pointer
  5.     ENDIF
  6.     Increment the source string pointer
  7. ENDFOR
  8.  
Don't forget to write a terminator to the destination string.
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#3: Sep 27 '06

re: would i have to copy pointers? (urgent?)


Quote:

Originally Posted by billyjay777

Hi,
I have a project where i must use pointers and write a function that strips all the white spacebar spaces out of an array. I also have to count how many white spaces i have. Well i figured out how to count all the white spaces i just don't know how to strip all the white spaces out of the sentence entered. I asked the proffessor and he said one way of doing it would be to crreate another array and copy it to the new array and when it hits a white space not to copy it in to the new array. I understand that,but i don't know how to use pointers to do it.so far i
have this

void stripWhite(char *str)
{
char str2[100];

char*p;
char *t;

}

any help would be great i need it asap.

Expand|Select|Wrap|Line Numbers
  1. void stripWhite(char* str)
  2. {
  3.     int j = 0;
  4.     for(int i=0; str[i] != '\0'; ++i)
  5.      {
  6.           if(str[i] != ' ')
  7.             str[j++] = str[i];     
  8.      }
  9.      str[j] = '\0';
  10.      cout<<str<<endl;
  11. }   
Newbie
 
Join Date: Sep 2006
Posts: 7
#4: Sep 27 '06

re: would i have to copy pointers? (urgent?)


Quote:

Originally Posted by vermarajeev

Expand|Select|Wrap|Line Numbers
  1. void stripWhite(char* str)
  2. {
  3.     int j = 0;
  4.     for(int i=0; str[i] != '\0'; ++i)
  5.      {
  6.           if(str[i] != ' ')
  7.             str[j++] = str[i];     
  8.      }
  9.      str[j] = '\0';
  10.      cout<<str<<endl;
  11. }   

That works but i have to use pointers.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#5: Sep 27 '06

re: would i have to copy pointers? (urgent?)


Quote:

Originally Posted by billyjay777

That works but i have to use pointers.

Adapt it

remember that

array[4]

is equivilent to

*(array+4)
Newbie
 
Join Date: Sep 2006
Posts: 7
#6: Sep 27 '06

re: would i have to copy pointers? (urgent?)


Quote:

Originally Posted by Banfa

Adapt it

remember that

array[4]

is equivilent to

*(array+4)

i am having a hard time understanding how to increment both pointers. I understand that they both start at the same point in there arrays, but i don't understand how to increment them to go to the next character in the array on the first array, but then tell the second array to skip the space.
that is the hardest thing i am dealing with so far i have this now.


void stripWhite(char* str)
{
char str2[100];
char *p;
p=str;
char*t;
t=str2;


{
if(*(p)!=' ')

{
t=p;

}



}
cout<<t<<endl;

}
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#7: Sep 28 '06

re: would i have to copy pointers? (urgent?)


What's so difficult? Always increment the pointer to the string with spaces. If you come upon a space, increment the number of spaces encountered. Otherwise, increment the pointer to the string without spaces.

If you are doing the operation in place, for some reason, then the number of spaces encountered is (ptr_with_spaces - ptr_no_spaces).
Member
 
Join Date: Sep 2006
Location: Hà Nội, Việt Nam
Posts: 41
#8: Sep 28 '06

re: would i have to copy pointers? (urgent?)


answer here:

void stripWhite(char* str)
{
char str2[100]="";
char *p;
p=str;
char*t;
t=str2;

int len=strlen(str);
int i=0;
int j=0;
while(i<len)
{
if(*(p+i)!=' ')
{
*(t+j)=*(p+i);
j++;
}
i++;
}
cout<<t<<endl;
}
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#9: Sep 28 '06

re: would i have to copy pointers? (urgent?)


Quote:

Originally Posted by billyjay777

i am having a hard time understanding how to increment both pointers. I understand that they both start at the same point in there arrays, but i don't understand how to increment them to go to the next character in the array on the first array, but then tell the second array to skip the space.
that is the hardest thing i am dealing with so far i have this now.


void stripWhite(char* str)
{
char str2[100];
char *p;
p=str;
char*t;
t=str2;


{
if(*(p)!=' ')

{
t=p;

}



}
cout<<t<<endl;

}

Hi, I think according to your question it just tells me to use pointers and as what I have posted there is a pointer too (i.e char* str). Also as Banfa said
arr[4] is equivalent to *(arr+4) that too is a pointer. So dont get panic about pointers. Use pointers whenever required. In your question it is never said that you have to use two pointers one for source and the other for destination.....I just tells me to use pointers....

Try something which is simple and the best solution.
Thankx
Newbie
 
Join Date: Sep 2006
Posts: 7
#10: Sep 28 '06

re: would i have to copy pointers? (urgent?)


Quote:

Originally Posted by vermarajeev

Hi, I think according to your question it just tells me to use pointers and as what I have posted there is a pointer too (i.e char* str). Also as Banfa said
arr[4] is equivalent to *(arr+4) that too is a pointer. So dont get panic about pointers. Use pointers whenever required. In your question it is never said that you have to use two pointers one for source and the other for destination.....I just tells me to use pointers....

Try something which is simple and the best solution.
Thankx

sorry for not stating right. And thanks for the help.
Reply