Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Array problem

Hi guys,

I was just wondering if a char array conatains "testing 123" and you want to remove the space between the testing and 123 how would you do this and what if there is more than one space between the testing and 123?

I have already got the code so it stops when it finds the g in testing but after that im not sure what to do. Thanks.
[358 byte] By [C++ Angel] at [2007-11-11 8:03:55]
# 1 Re: Array problem
try something like this...

char str[] = "testing 123";
char *newStr = (char *)malloc(sizeof(char) * strlen(str));

for(int x = 0, y = 0; x < strlen(str); x++, y++)
{
if(str[x] == ' ')
{
y--;
continue;
}
newStr[y] = str[x];
}
newStr[y] = '\0';

printf("%s\n%s\n", str, newStr);

x is this current position in str, y is the current position in newStr
add null char at end (newStr[y] = '\0')

hope this helps

Note: this also works if there is more than one space
Nathan87 at 2007-11-11 21:01:35 >
# 2 Re: Array problem
Thanks for your help! :)
C++ Angel at 2007-11-11 21:02:36 >
# 3 Re: Array problem
anyone know how 2 make sinusoidal graph using borland C++ builder.
the input is maximum value of Y-axis. can ignore the X-axis value.,
wary83 at 2007-11-11 21:03:39 >
# 4 Re: Array problem
graphics are difficult. You can learn about directx or opengl -- or see if your platform has a simple graphics tool or something? All you need is the ability to doodle on a canvas which is a common demo for compilers like bcb.
jonnin at 2007-11-11 21:04:45 >