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.
9 2500 Banfa 9,065
Expert Mod 8TB
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 -
FOREACH Character in the Source String
-
IF it is not a SPACE
-
Copy it to the destination string
-
Increment the destination string pointer
-
ENDIF
-
Increment the source string pointer
-
ENDFOR
-
Don't forget to write a terminator to the destination string.
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.
- void stripWhite(char* str)
-
{
-
int j = 0;
-
for(int i=0; str[i] != '\0'; ++i)
-
{
-
if(str[i] != ' ')
-
str[j++] = str[i];
-
}
-
str[j] = '\0';
-
cout<<str<<endl;
-
}
- void stripWhite(char* str)
-
{
-
int j = 0;
-
for(int i=0; str[i] != '\0'; ++i)
-
{
-
if(str[i] != ' ')
-
str[j++] = str[i];
-
}
-
str[j] = '\0';
-
cout<<str<<endl;
-
}
That works but i have to use pointers.
Banfa 9,065
Expert Mod 8TB
That works but i have to use pointers.
Adapt it
remember that
array[4]
is equivilent to
*(array+4)
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;
}
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).
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;
}
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Venkat |
last post by:
Hi All,
I need to copy strings from a single dimensional array to a double
dimensional array.
Here is my program.
#include <stdio.h>
#include <stdlib.h>
|
by: Chris K |
last post by:
I am relatively new to C++ and hope that this question is relevant. I
have spent some time at
the local library and some time on dejanews, but have no decided to go
ahead with my question,
since...
|
by: Gunnar G |
last post by:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?
I hope this will appear in future releases of STL :)
|
by: Alex |
last post by:
Entering the following in the Python shell yields
>>> help(dict.copy)
Help on method_descriptor:
copy(...)
D.copy() -> a shallow copy of D
>>>
|
by: PengYu.UT |
last post by:
The following program calls the normal constructor and the copy
constructor.
By calling the copy constuctor is redundandant, all I want is only a
vector of a trial object.
Is there any way to...
|
by: N. Spiker |
last post by:
I am attempting to receive a single TCP packet with some text ending with
carriage return and line feed characters. When the text is send and the
packet has the urgent flag set, the text read from...
|
by: nagrik |
last post by:
Hello group,
Last week I picked up a thread, which pointed out that if a copy
constructor is created with pointers
instead of reference, there is a danger of it going in infinite
recursion.
...
|
by: shuisheng |
last post by:
Dear All,
Is there any easy way to make sure all my object copies are deep copy
or shallow copy? I do not like to implement it in each class one by
one.
Thanks,
Shuisheng
|
by: 7stud |
last post by:
Here is some example code:
d = {"a":"hello", "b":}
x = d.copy()
d=10
print x
output:
{'a': 'hello', 'b': }
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |